我试图从一张工作表中获取一个范围,并添加该范围和一个字符串以在新工作表中创建一个新行。新工作表中的新行应该有字符串,然后是行。我虽然也许可以用数组来做到这一点,但它不起作用。
//tradeName is the name of the source file. The code is super long so i did not include it in this snippet.
var fileList = DriveApp.getFilesByName(tradeName);
var fileId = fileList.next().getId();
var sheetCurrent = SpreadsheetApp.getActiveSheet();
var source = SpreadsheetApp.openById(fileId);
var source_range = source.getRange("A20:AE20");
sheetCurrent.appendRow([tradeName,A20:AE20]);
最佳答案
您不能混合范围和字符串值。 正如您所注意到的,appendRow 中的参数必须是一个数组,因此执行您想要的操作的正确方法是在添加新行之前将字符串添加到数组中。 另一方面,当您检索行值时,您会得到一个数组的数组(二维数组),因此我们必须仅采用内部数组并使用 unshift 方法在其他元素之前添加一个元素。
代码可能是这样的:
function myFunction() {
var string = " ccccxxxx";
var sheetCurrent = SpreadsheetApp.getActiveSheet();
var source = SpreadsheetApp.openById(fileId);
var source_range = source.getRange("A20:AE20");
var source_rangeValue = source_range.getValues(); // 2D ARRAY
source_rangeValue[0].unshift(string);//add the string to the inner array
sheetCurrent.appendRow(source_rangeValue[0]);
}
关于javascript - 如何获取一行并将该行和文本字符串合并为一个新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40267049/