excel Office脚本- array.splice由于未知原因影响2个数组

piah890a  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(91)

我目前正在创建一个脚本来自动填充日历。现在,我被这个问题卡住了,我不知道为什么,但这个拼接似乎影响了我的2个数组。
下面是我拼接的代码部分:

// Remove element from temporary list
console.log('--before', ihListCountry, populatingIhListCountry);  
populatingIhListCountry.splice(j, 1);  
console.log('--after', ihListCountry, populatingIhListCountry);

字符串
下面是控制台中的结果:

--before  
(11) [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, …]  
(11) [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, …]  
--after  
(10) [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]  
(10) [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]  
--before  
(10) [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]  
(10) [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
--after  
(9) [Object, Object, Object, Object, Object, Object, Object, Object, Object]  
(9) [Object, Object, Object, Object, Object, Object, Object, Object, Object]


从长度上可以看出,两者都受到拼接的影响,有人知道这里的问题是什么吗?
Edit:下面是整个函数和我初始化数组的地方:

function populateIHListCalendar(
  workbook: ExcelScript.Workbook,
  currentWorksheet: ExcelScript.Worksheet,
  rangeText: string[],
  targetRange: ExcelScript.Range,
  ihListCountry: socTeam[]
) {
  let populatingIhListCountry: socTeam[] = ihListCountry;
  let availabilityWorksheet: ExcelScript.Worksheet = workbook.getWorksheets()[0];
  let availabilityRange: ExcelScript.Range = availabilityWorksheet.getUsedRange();
  //Loop through each cells
  for (let i: number = 0; i < rangeText.length; i++) {
    let ihAvailability: boolean = false;
    // Refill list if it's empty
    if (populatingIhListCountry.length == 0) {
      populatingIhListCountry = ihListCountry;
    }
    let availabilityColumn: string[][] = availabilityWorksheet.getUsedRange().getColumn(i).getTexts();
    let j: number = 0;
    let whileCycle: number = 0; 
    while(ihAvailability === false) {
      // Here I call a function comparing the IH Title with my Out of Office Calendar, returning true if the IH is available during this time period
      ihAvailability = compareAvailabilityAndIHList(availabilityColumn, populatingIhListCountry, j);
      if (ihAvailability === false) {
        j++;
        if (j === populatingIhListCountry.length && whileCycle === 0) {
          j = 0;
          whileCycle = 1;
        }
        else if (j === populatingIhListCountry.length && whileCycle === 1) {
          populatingIhListCountry = populatingIhListCountry.concat(ihListCountry);
          console.log('First cycle, no available IH in the remaining list available!');
          whileCycle = 2;
        }
        else if (j === populatingIhListCountry.length && whileCycle === 2) {
          console.log('Error! No available IH found!');
          break;
        }
      }
    }

    if (j === populatingIhListCountry.length && whileCycle === 2) {
      break;
    }

    // Add IH to calendar
    let calendarTable: ExcelScript.Table = currentWorksheet.getTables()[0];

    let columnRange = calendarTable.getColumn(rangeText[i]).getRange().getValues();
    let k: number = 0;
    while (columnRange[k] != undefined && columnRange[k].toString() != "") {
      k++;
    }
    if (calendarTable.getColumn(rangeText[i]).getRange().getCellCount() === j) {
      calendarTable.addRow();
    }
    currentWorksheet.getCell(k, calendarTable.getColumn(rangeText[i]).getIndex()).setValue(populatingIhListCountry[j].Title);

    // Remove element from temporary list
    console.log('--before', ihListCountry, populatingIhListCountry);
    populatingIhListCountry.splice(j, 1);
    console.log('--after', ihListCountry, populatingIhListCountry);
  }

  // IH Calendar is generated
}

qyswt5oh

qyswt5oh1#

吹代码是创建一个新的数组,但两个变量都是空数组。

let populatingIhListCountry: socTeam[] = ihListCountry;

字符串
一个代码片段来展示它,两个数组都是在第三行之后改变的。

const originalList: number[] = [1, 2, 3, 4, 5];
const copyList: number[] = originalList;
originalList[0] = 99
console.log(originalList)
console.log(copyList)

// Output
// [LOG]: [99, 2, 3, 4, 5] 
// [LOG]: [99, 2, 3, 4, 5]


你有几个选项来创建一个列表的独立副本(数组)。

// Using the spread operator:
const originalList1: number[] = [1, 2, 3, 4, 5];
const copyList1: number[] = [...originalList1];
originalList1[0] = 99
console.log(originalList1)
console.log(copyList1)

// Output
// [LOG]: [99, 2, 3, 4, 5] 
// [LOG]: [1, 2, 3, 4, 5] 

// Using the slice() method:
const originalList2: number[] = [1, 2, 3, 4, 5];
const copyList2: number[] = originalList2.slice();
originalList2[0] = 99
console.log(originalList1)
console.log(copyList1)

// Output
// [LOG]: [99, 2, 3, 4, 5] 
// [LOG]: [1, 2, 3, 4, 5]

相关问题