Google工作表为getRange返回null值

时间:2017-09-20 09:59:16

标签: google-apps-script google-sheets google-sheets-api

尝试编写一个函数,当选择一个菜单项时,在电子表格的打开表单底部调用和复制某些数据 - 当前正在逐行从“示例”表中调用数据,所以我试图将rowNumber传递给duplicateLastRow函数 - 目前在此它返回一个空值,我无法弄清楚原因。

我也非常愿意采用不同的方式,因为我的菜单有大约80个项目,因此有80个函数定义,我觉得500行代码过多,无法复制和粘贴命令。代码包含在下方,链接表在此公开:https://docs.google.com/spreadsheets/d/1yOJ8VdhbkbC9vw5or4geV-mv-PCk-WA86L8-F7-WGM8/edit?usp=sharing 脚本编辑器中的项目称为copyRowFromOriginToDestination,我正在处理的脚本名为testFileBlarg - Code2脚本是我正在尝试删除对话框提示和行的代码的工作但是巨大的版本。

提前干杯!

function onOpen() {
  var ui = SpreadsheetApp.getUi();

  ui.createMenu('testRow')
   .addSubMenu(ui.createMenu('Life Sciences')
   .addSubMenu(ui.createMenu('NRG')
    .addItem('Highlight/Alamy', 'callHANRG') 
    ))
  .addToUi();

}

function callHANRG(rowNumber){
  duplicateLastRow(3);
}

function duplicateLastRow(rowNumber){

  var originSheetName = "Example Rows";
  var ss = SpreadsheetApp.getActiveSpreadsheet(); // This is an object that represents the open spreadsheet
  var originSheet = ss.getSheetByName(originSheetName); // This object represents the sheet we are copying FROM
  var destinationSheet = ss.getActiveSheet() // getSheetByName(destinationSheetName); // This object represents the sheet we are copying TO

  destinationSheet.insertRowAfter(destinationSheet.getLastRow()); // This adds a row to the end of the destination sheet so it doesn't fill up!

  var originRow = originSheet.getRange(rowNumber, 1, 1, originSheet.getLastColumn() ); // This object represents the row to be copied
  var destinationRow = destinationSheet.getRange(destinationSheet.getLastRow()+1, 1); // This object represents the row we are copying to

  originRow.copyTo(destinationRow); // This actions the copying
 }

1 个答案:

答案 0 :(得分:0)

如果您想将数据从一个工作表复制到其他工作表,请参阅下面的代码以开始使用。

<强>更新

要将此功能用作复制数据的常用功能,例如 -

function callHANRG(rowNumber){
  copy(3, "A2:I20");
}

然后在Copy()函数中,

function Copy(rowNumber, rangeToBeCopied)
{
    var sss = SpreadsheetApp.openById('<SPREADSHEET_ID>');
    var ss = sss.getSheetByName('Front');
    var range = ss.getRange(rangeToBeCopied);
    var data = range.getValues();

   //If your copying data to different spreadsheet then only use this line or 
   //else use var ts = sss.getSheetByName('frontBackup'); and comment the two lines below.
    var tss = SpreadsheetApp.openById('<SPREADSHEET_ID>');
    var ts = tss.getSheetByName('frontBackup');
    ts.getRange(rowNumber, 1, data.length, data[0].length).setValues(data);
}