我试图从一个工作表中获取范围并添加范围加上一个字符串以在新工作表中创建一个新行。 新工作表中的新行应该包含字符串,然后是行。 我虽然也许我可以用阵列做到这一点,但它不起作用。
//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]);I am trying to get a range from one sheet and add the range plus a string to create a new row in a new sheet. The new row in the new sheet should have the string, then the row. I though maybe i could do this with an array but its not working.
//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的参数必须是一个数组,因此正确的方法是在添加新行之前将字符串添加到数组中。 另一方面,当您检索行值时,您将获得一个数组数组(一个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]); }You can't mix ranges and string values. As you noticed, the argument in appendRow must be an array, so the right way to do what you want is to add the string to the array before adding the new row. On the other hand, when you retrieve a row value you get an array of arrays (a 2D array) so we have to take only the inner array and use the unshift method to add an element before the others.
Code could be like this :
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]); }如何获取行并将行和文本字符串合并为一个新行(How to Get a row and combine the row and a text string into one new row)我试图从一个工作表中获取范围并添加范围加上一个字符串以在新工作表中创建一个新行。 新工作表中的新行应该包含字符串,然后是行。 我虽然也许我可以用阵列做到这一点,但它不起作用。
//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]);I am trying to get a range from one sheet and add the range plus a string to create a new row in a new sheet. The new row in the new sheet should have the string, then the row. I though maybe i could do this with an array but its not working.
//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的参数必须是一个数组,因此正确的方法是在添加新行之前将字符串添加到数组中。 另一方面,当您检索行值时,您将获得一个数组数组(一个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]); }You can't mix ranges and string values. As you noticed, the argument in appendRow must be an array, so the right way to do what you want is to add the string to the array before adding the new row. On the other hand, when you retrieve a row value you get an array of arrays (a 2D array) so we have to take only the inner array and use the unshift method to add an element before the others.
Code could be like this :
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]); }
发布评论