【发布时间】:2022-01-01 08:06:22
【问题描述】:
我正在尝试从源范围粘贴到目标范围,但在目标中跳过一列并保留其中的值(在附加的 sample 中,我想将值保留在 C 列(其他列)中。但可以'找不到保留这些值的方法或完全跳过该列。这是我正在使用的代码:
function appendSheet1ToSheet2() {
const ss = SpreadsheetApp.getActive();
const source = ss.getRange('Sheet1!A1:C');
const target = ss.getRange('Sheet2!A1:D');
appendUniquesToRange_(source, target);
}
function appendUniquesToRange_(sourceRange, targetRange) {
const dataToAppend = sourceRange.getValues().map(row => [row[0], row[1], ,row[2]]);
const existingData = targetRange.getValues()
const newData = existingData
.concat(dataToAppend)
.filter((row, rowIndex, array) =>
array
.map(tuple => tuple[0])
.indexOf(row[0]) === rowIndex && row[0] !== ''
);
targetRange
.offset(0, 0, newData.length, newData[0].length)
.setValues(newData);
}
我尝试将 target 定义为范围列表,但随后在第二个函数“TypeError: targetRangeList.getValues is not a function”中出现其他错误
const target = ss
.getRangeList(['Sheet2!A1:B', 'Sheet2!D1:D'])
.getRanges()
.map(range => range.getValues());
不知道怎么解决。
【问题讨论】:
标签: google-apps-script google-sheets spreadsheet