得到一个"找不到方法"谷歌脚本中的错误

时间:2016-08-17 13:22:57

标签: google-apps-script

我已经构建了下面的代码,当我运行它时,我不断收到错误消息 "找不到方法copyTo(sheet)"

当我尝试复制到目标工作表时,它正在上线。所有copyTo行都给出了相同的错误:source_sheet1.copyTo(target_sheet1);

编辑:我更改了代码,但仍然遇到同样的错误:

function UpdateCore() {

  //Set Target (Core sheets to use)
  var FindThisCore = "Compliance Spreadsheet Template- Sandton";
  var fileCore = DriveApp.searchFiles('title contains "' + FindThisCore+ '" and mimeType = "application/vnd.google-apps.spreadsheet"')
  while (fileCore.hasNext()) {
    var fileCoreMonth = fileCore.next();
    var target = SpreadsheetApp.openById(fileCoreMonth.getId());
    var getmonthsheet = target.getSheetByName("Run me")
    var range = getmonthsheet.getRange(2,2); 
    var NewMonth = range.getValue();}

  //Set compliance file to use  
  var FindThisMonth = "Compliance Spreadsheet Sandton " + NewMonth

  //Set the Source values (Monthly Values)
  var fileThisMonth = DriveApp.searchFiles('title contains "' + FindThisMonth + '" and mimeType = "application/vnd.google-apps.spreadsheet"') 
  while (fileThisMonth.hasNext()) {
     var fileMonth = fileThisMonth.next();
     var source = SpreadsheetApp.openById(fileMonth.getId());}

  CopyToSheet(source,target,"Guiding", "Guiding");
  CopyToSheet(source,target,"Learning", "Learning");
  CopyToSheet(source,target,"Bookkeeping", "Bookkeeping");
  CopyToSheet(source,target,"Fees", "Fees");
}

哪个电话

function CopyToSheet (SourceFile, TargetFile, SourceSheet, TargetSheet){

  var SourceSheetFile = SpreadsheetApp.openById(SourceFile.getId());
  var TargetSheetFile = SpreadsheetApp.openById(TargetFile.getId());
  var sourcetoread = SourceSheetFile.getSheetByName(SourceSheet);
  var targettowrite = TargetSheetFile.getSheetByName(TargetSheet);

  sourcetoread.copyTo(targettowrite);
}

我已经检查过它是通过清除目标和来源中的纸张来连接到正确的纸张 - 但是copyTo仍然无法正常工作。

2 个答案:

答案 0 :(得分:0)

我尝试通过此功能简化您的代码:

function test(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetundefined = ss.getSheetByName("badName");
  var sheetdefined = ss.getSheetByName("existingName");
  sheetdefined.copyTo(sheetdefined);
}

这样就会触发错误“无法找到方法copyTo ...”。

如果您尝试在未定义的对象上使用“copyTo”,它将抛出错误消息“无法调用方法copyTo of null”。 所以我想这只是意味着,如果您的工作表已经定义并且您的目标和来源不是同一个对象,您应该检查之前的那个,也许这样。

if(sheet != undefined && targetSheet != undefined &&
   sheet.getSheetId != targetSheet.getSheetId){
  sheet.copyTo(targetSheet);
}

答案 1 :(得分:0)

我发现了错误:我正在使用copyto方法寻址目标表,我将其更改为文件,它运行正常。变化很简单,在上面的函数中我改变了

sourcetoread.copyTo(targettowrite);

sourcetoread.copyTo(TargetSheetFile);

因为我希望它覆盖该名称的现有表格。我更改了整体功能以删除现有工作表,复制新名称,然后将其重命名为原始名称(而不是"复制到..."名称自动分配)。全新功能看起来像这样

function CopyToSheet (SourceFile, TargetFile, SourceSheet, TargetSheet){

  var SourceSheetFile = SpreadsheetApp.openById(SourceFile.getId());
  var TargetSheetFile = SpreadsheetApp.openById(TargetFile.getId());
  var sourcetoread = SourceSheetFile.getSheetByName(SourceSheet);
  var targettowrite = TargetSheetFile.getSheetByName(TargetSheet);
  TargetSheetFile.deleteSheet(targettowrite);

  sourcetoread.copyTo(TargetSheetFile);
  var RenameSheet = "Copy of " + TargetSheet; 
  var RenameThis = TargetSheetFile.getSheetByName(RenameSheet);
  RenameThis.setName(SourceSheet)

}