如何在谷歌应用程序脚本中设置整行时设置值?

问题描述

我一直在尝试运行这个,虽然 currentRow 有大约 23 列,但我只设置了第一列的值:

targetSheet.setActiveRange(targetSheet.getRange(maxIndex + 2,1))
 .setValue(currentRow);

我已经尝试过 seValues(),但它不起作用。

这是记录的 currentRow:

enter image description here

有灯吗?

解决方法

更新答案:

你有一个一维数组,让它变成二维:

targetSheet.getRange(maxIndex + 2,1,currentRow.length).setValues([currentRow])

  • 您不需要活动范围,您可以直接粘贴数据
  • 如果要使用 setValues 在工作表中粘贴多个值,输入 currentRow 需要是一个二维数组(getValues 的结果)。

以下示例从 A1:W1 复制值并将它们粘贴到 A2:W2

function myFunction() {
  const ss = SpreadsheetApp.getActive();
  const targetSheet = ss.getSheetByName('target'); // put the name of your sheet
  const currentRow = targetSheet.getRange(1,23).getValues(); // range A1:W1
  targetSheet.getRange(2,23).setValues(currentRow); // set values to A2:W2
}