首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Iterator只在嵌套的For循环中重复其最大电位值(Office脚本)

Iterator只在嵌套的For循环中重复其最大电位值(Office脚本)
EN

Stack Overflow用户
提问于 2022-06-27 01:45:34
回答 1查看 105关注 0票数 2

我正在使用Power Automate & Office脚本构建一种在Excel中进行批处理创建的方法。本质上是一种向Excel中的脚本发送值数组的方法,然后该脚本用数组中的所有值更新Excel表。Office脚本使用JavaScript语言。

但是,我开始在我的结果中得到错误,它只是在流发送的数组中张贴重复的值:

云流没有什么问题,我检查了Office脚本。此错误发生在在内存表中生成新行的部件上。具体而言,这一代码:

代码语言:javascript
运行
复制
    //-1 to 0 index the RowNum
RowNum = RowNum - 1;
//Iterate through each object item in the array from the flow
for (let i = 0; i < CreateData.length; i++) {
  //Create an empty row at the end of the 2D table array & increase RowNum by 1 for the next line updating that row
  TableData.push(EmptyRow);
  RowNum++;
  //Iterate through each item or line of the current object
  for (let j = 0; j < Object.keys(CreateData[i]).length; j++) {
    //Create each value for each item or column given
    TableData[RowNum][Number(Object.keys(CreateData[i])[j])] = CreateData[i][Object.keys(CreateData[i])[j]];
  }
}

这是非常奇怪的,因为本质上相同的代码适用于另一种批处理创建更大表的方法,在这些表中,我无法在内存中完成任务:

代码语言:javascript
运行
复制
    //Iterate through each object item in the array from the flow
for (let i = 0; i < CreateData.length; i++) {
  //Create an empty row at the end of the 2D table array & increase RowNum by 1 for the next line updating that row
  table.addRow()
  RowNum = RowNum + 1
  //Iterate through each item or line of the current object
  for (let j = 0; j < Object.keys(CreateData[i]).length; j++) {
    //Create each value for each item or column given
    TableRange.getCell(RowNum, Number(Object.keys(CreateData[i])[j])).setValue(CreateData[i][Object.keys(CreateData[i])[j]])
  }
}

类似的代码适用于批处理更新脚本中的类似过程:

代码语言:javascript
运行
复制
    //Iterate through each object item in the array from the flow
for (let i = 0; i < UpdatedData.length; i++) {
  //If the record's Primary Key value is found continue, else post to error log
  if (ArrayPK.indexOf(UpdatedData[i].PK) > 0) {
    //Get the row number for the line to update by matching the foreign key from the other datasource to the primary key in Excel
    RowNum = ArrayPK.indexOf(UpdatedData[i].PK)

    //Iterate through each item or line of the current object
    for (let j = 0; j < Object.keys(UpdatedData[i]).length - 1; j++) {
      //Update each value for each item or column given
      TableData[RowNum][Number(Object.keys(UpdatedData[i])[j])] = UpdatedData[i][Object.keys(UpdatedData[i])[j]]
    }
  }

我看不出有什么逻辑上的问题。

实际上,如果我将代码中的i引用更改为静态数字,以便它每次引用相同的项,那么它就会重复其他静态行索引中的值。就好像在这种特殊情况下,当i引用在嵌套循环中时,它只是从Power自动传递给它的值数组中的最后一个值(通常是这个循环中的最大值),并且在每个循环运行中都不会从这个值中更改,即使每次运行都应该添加一个到I (i++)。

为什么会发生这种情况?

编辑:如果我用"“、"”之类的空数组手动填写EmptyRow位,那么错误就会消失,并正常进入新行。但这毫无意义。我提取了两个手动输入&空白行的auto0generate条目,它们都具有完全相同的结构、输出和长度,

EN

回答 1

Stack Overflow用户

发布于 2022-06-28 04:19:03

如果我尝试为内存中的表使用EmptyRow数组变量,它似乎就不起作用了。我必须调整代码的这一部分,以便在每个循环中计算一个新的空白数组。见以下更新的代码:

代码语言:javascript
运行
复制
    //Iterate through each object item in the array from the flow
for (let i = 0; i < CreateData.length; i++) {
  //Create an empty row at the end of the 2D table array & increase RowNum by 1 for the next line updating that row
  TableData.push(",".repeat(TableNumberOfColumns - 1).split(","));
  RowNum++;

  //Iterate through each item or line of the current object
  for (let j = 0; j < Object.keys(CreateData[i]).length; j++) {
    //Create each value for each item or column given
    TableData[RowNum][Number(Object.keys(CreateData[i])[j])] = CreateData[i][Number(Object.keys(CreateData[i])[j])];
  }
}

https://powerusers.microsoft.com/t5/Power-Automate-Cookbook/Excel-Batch-Create-Update-and-Upsert/td-p/1624706

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72766311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档