【问题标题】:How to run a function with optional argument/parameter in Google Apps Script如何在 Google Apps 脚本中运行带有可选参数/参数的函数
【发布时间】:2018-07-01 13:05:31
【问题描述】:

我正在尝试在 Google Apps 脚本中运行一个具有可选参数/参数的函数。该函数将创建一个 Google 文档,如果用户选择 Yes,还将创建一个 Google 幻灯片。我的问题是在调用extractReplace函数的时候,会因为copySlide没有定义而报错。

这是我调用函数的方式:

  var copyId = DriveApp.getFileById(documentTemplate)
      .makeCopy(documentName + folderName, nsdestinationFolder) 
      .getId(); 
  var copyDocument = DocumentApp.openById(copyId); 
  var copyBody = copyDocument.getActiveSection(); 

  //call function to create technical slide proposal 
  if(createTemplate == "Yes"){
    createSlideProposal();
  } 
  else{
    extractReplace(copyBody, copySlide = 0); 
  } 

我尝试了 copySlide = 0, false, null,但它不起作用,因为在传递到 copySlide.replaceAllText 行时未定义 copySlide。还有另一个函数包含copySlide 变量,但只有当用户选择“是”来创建幻灯片提案(powerpoint)时,它才会被调用并传递给extractReplace 函数。

这是使用copySlide的函数

function extractReplace(copyBody, copySlide) {
  var result  = ""; //result variable string = 0 
  var ss = SpreadsheetApp.openById("IDKEYHERE");
  var firstDatabasesheet = ss.getSheets()[0];
  var data = firstDatabasesheet.getDataRange().getValues();   

  data.forEach(function(row) {
    if (row[32] == TMSreferenceNumber){
      result = row[2] || row[16]; //chooses whichever cell that has something in it
      copyBody.replaceText('repbranchOffice', result); //replaces text in document template
      result = row[3] || row[17]; 
      copyBody.replaceText('repsalesContact', result);
      result = row[4] || row[18];
      copyBody.replaceText('repbranchReferenceNumber', result);
      result = row[5] || row[19];
      copyBody.replaceText('rependUser', result);    
      copySlide.replaceAllText('rependUser', result); //for slide
    }
...

【问题讨论】:

    标签: function google-apps-script parameter-passing


    【解决方案1】:

    使用可选参数时,如果不提供默认值,通常需要测试它们是否缺失。

    在调用站点,如果参数是可选的且对您不重要,您通常希望能够省略参数:foo(rq)foo(rq, someParamIdontCareAbout)foo(rq, 0),因为它降低了调用站点代码的复杂性.

    在带有可选参数的函数中,如果需要(例如原语、标志)或保护与可选类参数交互的代码块(如对Slide 对象的引用),您希望初始化这些值。

    初始化:

    function foo(required, startVal = /* something */) {
      ...
    }
    function foo(required, startVal) {
      if (startVal === undefined) {
        /* more than a one-statement initialization */
      }
      ...
    }
    

    格挡卫士

    function foo(required, objectRef) {
      /* Code that does not need the object reference */
      if (objectRef) {
        /*
         * Code that needs the object reference
         * objectRef.someMethod(...);
         */
      }
      /* More code that does not need the object reference */
    }
    

    【讨论】:

      【解决方案2】:

      替换

        extractReplace(copyBody, copySlide = 0); 
      

        extractReplace(copyBody, 0); 
      

      【讨论】:

      • 我之前尝试过,但后来收到以下消息错误-->“TypeError: Cannot find function replaceAllText in object 0.”
      猜你喜欢
      • 2022-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-10
      • 2012-08-21
      相关资源
      最近更新 更多