getLastRow没有返回结果

时间:2019-06-12 00:34:51

标签: google-apps-script google-sheets

我有这个简单的代码,可以将转置范围复制到从第14列开始的另一张工作表的最后一行

   function copyAllInRange(){
    var ss = SpreadsheetApp.getActiveSpreadsheet()
    var sheet = ss.getActiveSheet();
    var destsheet = ss.getSheetByName('Database')
    var destrow = destsheet.getLastRow()

    var rangeToCopy = sheet.getRange(53, 4, sheet.getMaxRows(), sheet.getRange('54:54').getLastColumn());
    rangeToCopy.copyTo(destsheet.getRange(destrow,14),SpreadsheetApp.CopyPasteType.PASTE_VALUES,true);

    }

它按原样返回零结果。如果我将destrow从公式更改为简单2(即现在的最后一个空行),则可以正常工作。为什么它不返回目标表中的最后一行索引?

2 个答案:

答案 0 :(得分:0)

尝试一下:

function copyAllInRange(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var destsheet = ss.getSheetByName('Database');
  var destrow = destsheet.getLastRow();
  var rangeToCopy = sheet.getRange(53,4,sheet.getMaxRows()-52,sheet.getLastColumn()-3);//are really sure that you want to use getMaxRows() here cause that seems weird to me. 
  rangeToCopy.copyTo(destsheet.getRange(destrow + 1,14),SpreadsheetApp.CopyPasteType.PASTE_VALUES,true);
}

答案 1 :(得分:0)

ss.getSheetByName('Database')很可能返回null,因为getLastRow()将为任何工作表返回1或更大的数字。