脚本尽早终止而执行脚本不会失败

时间:2019-06-09 17:37:03

标签: google-apps-script google-sheets

我的脚本由可安装的触发器执行,该触发器检测单独电子表格上的编辑。一切正常,直到将已编辑范围分配给变量的行为止。执行记录仅表示执行成功,因此我对执行失败的原因感到困惑。

这是直到脚本终止之前相关功能的代码。

function  IncrementDecrement(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var range = e.range;
  var masterSS = SpreadsheetApp.openById("ID");
  var i;

   Logger.log("1");

  //Check if edit occurred in valid range, and whether the edit occurred on a job or reception sheet (indicated by a 0 or 1 in L1)
  if ((range.getColumn() !== 3) && (range.getColumn() !== 4) && (range.getColumn() !== 5) && (range.getColumn() !== 6)) return;
  if ((sheet.getRange('L1').getValue() !== 0) && (sheet.getRange('L1').getValue() !== 1)) return;


  var invsheet = masterSS.getSheetByName("Inventory");
  var q;
  var deltaQ;
  var sku;
  var nextCell;
  var currentCell;
  var cellLink;

  Logger.log("2");
  currentCell = range;

这是执行记录。

[19-06-09 10:18:30:896 PDT] Starting execution
[19-06-09 10:18:30:903 PDT] SpreadsheetApp.getActiveSpreadsheet() [0 seconds]
[19-06-09 10:18:30:903 PDT] Spreadsheet.getActiveSheet() [0 seconds]
[19-06-09 10:18:30:904 PDT] SpreadsheetApp.getActiveRange() [0 seconds]
[19-06-09 10:18:30:904 PDT] Range.getRow() [0 seconds]
[19-06-09 10:18:30:904 PDT] Range.getLastRow() [0 seconds]
[19-06-09 10:18:30:904 PDT] Range.getColumn() [0 seconds]
[19-06-09 10:18:30:904 PDT] Range.getLastColumn() [0 seconds]
[19-06-09 10:18:30:982 PDT] SpreadsheetApp.openById([ID]) [0.077 seconds]
[19-06-09 10:18:30:983 PDT] Logger.log([1, []]) [0 seconds]
[19-06-09 10:18:30:983 PDT] Range.getColumn() [0 seconds]
[19-06-09 10:18:30:984 PDT] Sheet.getRange([L1]) [0 seconds]
[19-06-09 10:18:31:068 PDT] Range.getValue() [0.083 seconds]
[19-06-09 10:18:31:068 PDT] Spreadsheet.getSheetByName([Inventory]) [0 seconds]
[19-06-09 10:18:31:069 PDT] Logger.log([2, []]) [0 seconds]
[19-06-09 10:18:31:070 PDT] Execution succeeded [0.168 seconds total runtime]

以下是完整的代码,以备不时之需。

function main(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var range = e.range;
  var newId;
  var newName;
  var hyperlinkString;

  //Check if edit occurred in relevant range
  if((range.getColumn() !== 1) && (range.getColumn() !== 2)) return;

  //Check if both columns were filled after edit
  if((range.getColumn() == 1) && (range.offset(0,1).isBlank() == true)) return;
  if((range.getColumn() == 2) && (range.offset(0,-1).isBlank() == true)) return;

  //Get new document name from concatenate formula in column H
  if(range.getColumn() == 1) newName = range.offset(0,7).getValue();
  if(range.getColumn() == 2) newName = range.offset(0,6).getValue();

  //Check whether the edits occurred on the jobs list or receptions list (indicated by '2' or '3' in L1)
  //Calls function to create new job sheet or reception sheet from template, gets ID of new spreadsheet
  if(((range.getColumn() == 1) && (range.offset(0,3).isBlank() == true)) || ((range.getColumn() == 2) && (range.offset(0,2).isBlank() == true))) { 
    if(sheet.getRange('L1').getValue() == 2) newId = newJob();
    if(sheet.getRange('L1').getValue() == 3) newId = newReception();
  }
  else {
    if(range.getColumn() == 1) {
      hyperlinkString = range.offset(0,3).getFormula();
      newId = hyperlinkString.substring(77,121);
    }
    if(range.getColumn() == 2) {
      hyperlinkString = range.offset(0,2).getFormula();
      newId = hyperlinkString.substring(77,121);
    }
  }

  //Set the name of the new spreadsheet
  SpreadsheetApp.openById(newId).rename(newName);

  //Enter name and date information onto new spreadsheet
  if(range.getColumn() == 1) {
    SpreadsheetApp.openById(newId).getSheets()[0].getRange('B1').setValue(range.getValue());
    SpreadsheetApp.openById(newId).getSheets()[0].getRange('B2').setValue(range.offset(0,1).getValue());
  }
  if(range.getColumn() == 2) {
    SpreadsheetApp.openById(newId).getSheets()[0].getRange('B1').setValue(range.offset(0,-1).getValue());
    SpreadsheetApp.openById(newId).getSheets()[0].getRange('B2').setValue(range.getValue());
  }


  //Creates hyperlink to new spreadsheet
  if (range.getColumn() == 1) range.offset(0,3).setFormula("=HYPERLINK(\"" + SpreadsheetApp.openById(newId).getUrl() +"\",\"Click here for itemization\")");
  if (range.getColumn() == 2) range.offset(0,2).setFormula("=HYPERLINK(\"" + SpreadsheetApp.openById(newId).getUrl() +"\",\"Click here for itemization\")");

  //Sort list descending from most recent date
  sheet.getRange("A3:D1000").sort({column: 2, ascending: false});
}

function newJob() {
  //Open template
  var jobTemplateSS = SpreadsheetApp.openById("ID");

  //Create new spreadsheet from copy of template spreadsheet
  var newSS = jobTemplateSS.copy("Untitled Job");

  //Get folder
  var jobFolder = DriveApp.getFolderById("ID");

  //Get ID of new file
  var newSSFile = DriveApp.getFileById(newSS.getId());

  //Copy file to the correct directory and delete the instance created in root
  jobFolder.addFile(newSSFile);
  DriveApp.getRootFolder().removeFile(newSSFile);

  createInstallableTrigger('IncrementDecrement',newSS.getId());

  //Pass ID of new spreadsheet back to calling function
  return(newSS.getId());
} 

function newReception() {
  //Open template
  var receptionTemplateSS = SpreadsheetApp.openById("ID");

  //Create new spreadsheet from copy of template spreadsheet
  var newSS = receptionTemplateSS.copy("Untitled Reception");

  //Get folder
  var receptionFolder = DriveApp.getFolderById("ID");

  //Get ID of new file
  var newSSFile = DriveApp.getFileById(newSS.getId());

  //Copy file to the correct directory and delete the instance created in root
  receptionFolder.addFile(newSSFile);
  DriveApp.getRootFolder().removeFile(newSSFile);

  createInstallableTrigger('IncrementDecrement',newSS.getId());

  //Pass ID of new spreadsheet back to calling function
  return(newSS.getId());
}

function createInstallableTrigger(funcName,ssId) {
  Logger.log("1");
  if(!isTrigger()) {
    ScriptApp.newTrigger(funcName).forSpreadsheet(ssId).onEdit().create();
  }
}

function isTrigger(funcName){
  Logger.log("2");
  var r=false;
  if(funcName){
    var allTriggers=ScriptApp.getProjectTriggers();
    for(var i=0;i<allTriggers.length;i++){
      if(funcName==allTriggers[i].getHandlerFunction()){
        r=true;
        break;
      }
    }
  }
  return r;
}

function  IncrementDecrement(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var range = e.range;
  var masterSS = SpreadsheetApp.openById("ID");
  var i;

   Logger.log("1");

  //Check if edit occurred in valid range, and whether the edit occurred on a job or reception sheet (indicated by a 0 or 1 in L1)
  if ((range.getColumn() !== 3) && (range.getColumn() !== 4) && (range.getColumn() !== 5) && (range.getColumn() !== 6)) return;
  if ((sheet.getRange('L1').getValue() !== 0) && (sheet.getRange('L1').getValue() !== 1)) return;


  var invsheet = masterSS.getSheetByName("Inventory");
  var q;
  var deltaQ;
  var sku;
  var nextCell;
  var currentCell;
  var cellLink;

  Logger.log("2");
  currentCell = range;

  if(range.getColumn == 3) {

    q = currentCell.getValue();
    sku = currentCell.offset(0,-2).getValue();

    currentCell = currentCell.offset(0,8);

    if (currentCell.isBlank() == true) {
    currentCell.setValue(0);
    }

    currentCell = currentCell.offset(0,1);
    currentCell.setValue(q - currentCell.offset(0,-1).getValue());
    deltaQ = currentCell.getValue();
    cellLink = currentCell;

    currentCell = invsheet.getRange('B7');
    while (currentCell.isBlank()==false) {
      if(currentCell.getValue() == sku) {
        if (sheet.getRange('L1').getValue() == 0) {
          currentCell.offset(0,2).setValue(currentCell.offset(0,2).getValue() - deltaQ);
          currentCell.offset(0,3).setValue(currentCell.offset(0,3).getValue() - deltaQ);

          for(i=3; i<1000; i++) {
            if(masterSS.getSheetByName("Job List").getRange(i,1).getValue() == sheet.getRange(1,2).getValue()) {
              masterSS.getSheetByName("Job List").getRange(i,1).offset(0,2).setValue(sheet.getRange(4,6).getValue());
              break;
            }
          }
        }
        if (sheet.getRange('L1').getValue() == 1) {
          currentCell.offset(0,2).setValue(currentCell.offset(0,2).getValue() + deltaQ);
          currentCell.offset(0,3).setValue(currentCell.offset(0,3).getValue() + deltaQ);

          for(i=3; i<1000; i++) {
            if(masterSS.getSheetByName("Reception List").getRange(i,1).getValue() == sheet.getRange(1,2).getValue()) {
              masterSS.getSheetByName("Reception List").getRange(i,1).offset(0,2).setValue(sheet.getRange(4,7).getValue());
              break;
            }
          }
        }
      }
      nextCell = currentCell.offset(1,0);
      currentCell = nextCell;
    }


    currentCell = cellLink;
    currentCell = currentCell.offset(0,-1);
    currentCell.setValue(q);

    currentCell = currentCell.offset(0,-8);
  }



}

0 个答案:

没有答案