脚本问题与数组引用

时间:2017-04-22 19:46:20

标签: google-apps-script

请原谅我得到帮助的帖子的长度,但这个人上周没有上网所以我想在这里尝试最后几个问题。感谢您提供任何额外帮助。

这是我的原始剧本。

function ImportDataRange() {

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName("AM trip"); //To Sheet Name
 var ssraw = SpreadsheetApp.openById("1n4**4HzvVS****RQhPHndUbn0N0nkpOE5NpO2U"); // From Spreadsheet ID
 var sheetraw = ssraw.getSheetByName("AM trip"); // From Sheet name
 var range = sheetraw.getRange("B9:U38");
 var data = range.getValues();
 sheet.getRange("C1").setValues(data)
}

My adjusted script:-
function ImportDataRange() {

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName('AM trip'); //To Sheet Name
 var ssraw = SpreadsheetApp.openById('18h-UAy10EvANHZXLebspZsUbE2fisyEBAFnUYV9JBHs'); // From Spreadsheet ID
 var sheetraw = ssraw.getSheetByName('AM trip'); // From Sheet name
 var range = sheetraw.getRange('B7:U38');
 var data = range.getValues();
 var lastRow = sheet.getRange(sheet.getLastRow()+1,3).setValue(data);

}}
This takes B7 from the original spreadsheet and places it in the next blank row down and in Column C but not B7:U38, if i change setValue(data) to setValues(Data) I get the range height error.

Then below, I got from you and below the script is the error it produces:-
function ImportDataRange() {

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName('AM trip'); //To Sheet Name
 var ssraw = SpreadsheetApp.openById('18hAy10EvANHZXLebspZsUbE2fisyEBAFnUYV9JBHs'); // From Spreadsheet ID
 var sheetraw = ssraw.getSheetByName('AM trip'); // From Sheet name
 var range = sheetraw.getRange('B7:U38');
 var data = range.getValues();
 var lastRow = sheet.getLastRow()+1;
  setValues(sheet, lastRow, 3, data);

1 个答案:

答案 0 :(得分:0)

错误的原因是范围高度应与输入数据的高度相匹配。 你必须像这样修改代码

function ImportDataRange() {

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName('AM trip'); //To Sheet Name
 var ssraw = SpreadsheetApp.openById('18hAy10EvANHZXLebspZsUbE2fisyEBAFnUYV9JBHs'); // From Spreadsheet ID
 var sheetraw = ssraw.getSheetByName('AM trip'); // From Sheet name
 var range = sheetraw.getRange('B7:U38');
 var data = range.getValues();
 var lastRow = sheet.getLastRow()+1;
 sheet.getRange(lastRow,3,data.length,data[0].length).setValues(data)
}

基本上,使用array.length获取数据数组的长度可以得到行数。您可以在here

上找到更多详细信息

可以找到有关setValues的详细信息here,同样可以找到有关getRange的详细信息here

希望有所帮助