在 Google 表格中,为什么我录制的宏在调用时还会 运行 其他两个脚本?

In Google Sheets, why does my recorded macro also run two other scripts when invoked?

我在表格里录制了一个宏,目的是添加条件格式的色阶。它工作正常,但是当它完成时我得到两个弹出窗口说 "Done" 。我已经将这些追溯到我过去使用过的另外两个应用程序脚本。 (注意:这些是脚本,不是录制的宏)

问。为什么,当我使用指定的键盘快捷键调用宏时,我是否也会得到其他脚本的弹出窗口?脚本本身似乎 运行 不完全(因为它们修改的范围没有改变)。

这是录制的宏:

function Addcolourscale() {
  var spreadsheet = SpreadsheetApp.getActive();
  var conditionalFormatRules = spreadsheet.getActiveSheet().getConditionalFormatRules();
  conditionalFormatRules.push(SpreadsheetApp.newConditionalFormatRule()
  .setRanges([spreadsheet.getActiveRange()])
  .whenCellNotEmpty()
  .setBackground('#B7E1CD')
  .build());
  spreadsheet.getActiveSheet().setConditionalFormatRules(conditionalFormatRules);
  conditionalFormatRules = spreadsheet.getActiveSheet().getConditionalFormatRules();
  conditionalFormatRules.splice(conditionalFormatRules.length - 1, 1, SpreadsheetApp.newConditionalFormatRule()
  .setRanges([spreadsheet.getActiveRange()])
  .setGradientMinpoint('#57BB8A')
  .setGradientMaxpoint('#FFFFFF')
  .build()); 
  spreadsheet.getActiveSheet().setConditionalFormatRules(conditionalFormatRules);
  conditionalFormatRules = spreadsheet.getActiveSheet().getConditionalFormatRules();
  conditionalFormatRules.splice(conditionalFormatRules.length - 1, 1, SpreadsheetApp.newConditionalFormatRule()
  .setRanges([spreadsheet.getActiveRange()])
  .setGradientMinpoint('#57BB8A')
  .setGradientMidpointWithValue('#FFD666', SpreadsheetApp.InterpolationType.PERCENTILE, '50')
  .setGradientMaxpoint('#E67C73')
  .build());
  spreadsheet.getActiveSheet().setConditionalFormatRules(conditionalFormatRules);
};

这是生成弹出窗口的脚本之一,由于它的最后两行,当上述宏完成时。这是我从网上资源改编的,所以不太了解。

function CopyClientChannelUseBack(){
    /* Edit the vars below this line for your needs */
    var sourceSheet  = "12 mth Client channel use" ;  // Enter the name of the sheet with the source data
    var sourceRange = "A19:S29" ; // Enter the range of the cells with the source data
    var targetSheet = "12 mth Client channel use" ; // Enter the name of the target sheet  
    var targetRange = "A20:S30" ; // Enter the range of cells you wish to copy data to. Note this must be same size as source range.
    /* No need to edit below this point */  


    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName(sourceSheet);
    var values = sheet.getRange(sourceRange).getValues();
    ss.getSheetByName(targetSheet).getRange(targetRange).setValues(values);
  SpreadsheetApp.flush()

      /* Edit the vars below this line for your needs */
    var sourceSheet  = "12 mth Client channel use" ;  // Enter the name of the sheet with the source data
    var sourceRange = "A49:N59" ; // Enter the range of the cells with the source data
    var targetSheet = "12 mth Client channel use" ; // Enter the name of the target sheet  
    var targetRange = "A50:N60" ; // Enter the range of cells you wish to copy data to. Note this must be same size as source range.
    /* No need to edit below this point */ 

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName(sourceSheet);
    var values = sheet.getRange(sourceRange).getValues();
    ss.getSheetByName(targetSheet).getRange(targetRange).setValues(values);
  SpreadsheetApp.flush()      
  SpreadsheetApp.getUi().alert("Done")
}

清单文件没有引用脚本,只有宏。

{
  "timeZone": "Europe/London",
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER",
  "sheets": {
    "macros": [{
      "menuName": "Add colour scale",
      "functionName": "Addcolourscale",
      "defaultShortcut": "Ctrl+Alt+Shift+9"
    }]
  }
}

好的。我找出问题所在。与我粘贴的代码无关。事实证明,我在其他脚本的末尾添加了一些代码,实际上是在末尾,在该函数的右 } 括号之外。所以那几行也是 运行 。看起来 GAS 遍历了所有可用的脚本,寻找刚刚被调用的脚本。因为有函数外的代码,所以也被执行了。