【问题标题】:Run script on Sheet(tab) selection/change在工作表(选项卡)选择/更改上运行脚本
【发布时间】:2021-02-15 07:33:12
【问题描述】:

我想知道是否有一种方法可以在每次选择工作表中的特定工作表时自动触发脚本。

环顾四周,我发现了 onSelectionChange 函数,但我无法将它用于我的范围。脚本草稿如下。

function onSelectionChange(e) 
{ // Run update script when sheet is selected 
 var sheet = e.sheet; 
 var ssname = SpreadsheetApp.getActiveSheet().getName(); 
 if(ssname === "Projects") {
    UpdateProjects(); 
    }
} 

任何建议将不胜感激。谢谢!

【问题讨论】:

    标签: google-apps-script triggers


    【解决方案1】:

    我相信你的目标如下。

    • 您希望在激活特定工作表时运行脚本。
    • 您希望使用 Google Apps 脚本实现此目的。

    我认为在这种情况下,你的目标可以通过修改this thread的示例脚本来实现。

    用法

    1. 请将以下示例脚本复制并粘贴到 Google 电子表格的容器绑定脚本中,并保存该脚本。

    2. 请重新打开 Google 电子表格。

      • 由此,onOpen 运行,当前工作表被放入 PropertiesService。
      • 很遗憾,在当前阶段,onSelectionChange 的事件对象似乎没有关于标签更改的信息。所以为了检测tab的变化,我使用了PropertiesService。
    3. 然后,请在 Google 电子表格中选择其他表格。

      • 由此,onSelectionChange 由 onSelectionChange 事件触发器运行,当激活的工作表包含在specificSheetNames 中时,运行e.source.toast("Run script"); 的脚本。

    示例脚本:

    function onOpen(e) {
      const prop = PropertiesService.getScriptProperties();
      const sheetName = e.range.getSheet().getSheetName();
      prop.setProperty("previousSheet", sheetName);
    }
    
    function onSelectionChange(e) {
      const specificSheetNames = ["Projects"]; // Please set the sheet names you want to run the script.
    
      const prop = PropertiesService.getScriptProperties();
      const previousSheet = prop.getProperty("previousSheet");
      const range = e.range;
      const a1Notation = range.getA1Notation();
      const sheetName = range.getSheet().getSheetName();
      if (!specificSheetNames.includes(sheetName)) return;
      
      // When the specifc sheet names are activated, this script is run.
      e.source.toast("run script"); // This is a sample script.
    
      prop.setProperty("previousSheet", sheetName);
    }
    
    • 在此示例脚本中,当工作表更改为“项目”时,if 语句中的脚本将运行。

    注意:

    • 很遗憾,从您的脚本中,我无法理解您脚本中的 UpdateProjects()。在当前阶段,由于授权,所有方法都不能以onSelectionChange 运行。因为这个触发器是简单的触发器。所以请注意这一点。所以作为检查上述脚本的简单测试,首先请使用e.source.toast("run script");

    参考资料:

    【讨论】:

    • 田池,非常感谢您的回复。我尝试了您共享的相同代码,但没有任何反应。我想知道是否有 type-o 或其他东西......:/
    • @Andre8426 感谢您的回复。我带来的不便表示歉意。不幸的是,当我通过测试此示例脚本将工作表打开到“项目”时,e.source.toast("run script") 的示例脚本将运行。所以我无法复制你的情况。我为此道歉。例如,在您使用此脚本之前,请确认您的 Google Apps 脚本项目中是否不存在相同的函数名称。
    • @Andre8426 并且,请重新打开 Google 电子表格。这样,初始工作表名称被保存。然后,请打开“项目”表。这样,您可以确认示例脚本已运行。如果您测试了此流程并且示例脚本未运行,您能否提供当前的 Google 电子表格,包括当前脚本?借此,我想确认您的情况。
    • @Andre8426 顺便说一句,我无法理解I am wondering if there's a type-o or something else。我为我糟糕的英语水平深表歉意。可以问一下具体情况吗?
    • @Andre8426 感谢您的回复。我带来的不便表示歉意。关于The only thing that comes to my mind is that I also have other onOpen and onEdit functions: do you think this could be the problem? should I populate them all together in a single function?,当您创建新的电子表格并放置我的示例脚本并使用该脚本时,您会得到什么结果?从这个结果,想想你的问题怎么样?因为您的这个问题是针对这种情况的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 2012-05-23
    相关资源
    最近更新 更多