我正在尝试从一个工作表中获取范围并添加范围加上一个字符串以在新工作表中创建新行。新工作表中的新行应该包含字符串,然后是行。我虽然也许我可以用阵列做到这一点,但它不起作用。
//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]);
答案 0 :(得分:1)
您不能混合范围和字符串值。
正如您所注意到的,appendRow
中的参数必须是一个数组,因此正确的方法是在添加新行之前将字符串添加到数组。
另一方面,当您检索行值时,您将获得一个数组数组(一个2D数组),因此我们只需要使用内部数组并使用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]);
}