【问题标题】:How to detect user changing sheet?如何检测用户更改表?
【发布时间】:2016-06-08 13:02:25
【问题描述】:

如何在电子表格中打开特定工作表时检测和触发自定义操作?

我在https://developers.google.com/apps-script/guides/triggers/events中找不到合适的功能

【问题讨论】:

标签: google-apps-script google-sheets


【解决方案1】:

要按照 Mogsdad 的建议进行轮询,您可以在侧边栏(或任何 UI 元素)中使用此代码

$(function() {
   /**
   * On document load, assign click handlers to each button and try to load the
   * user's origin and destination language preferences if previously set.
   */
   poll();
}

function poll(interval){
    interval = interval || 2000;
    setTimeout(function(){
    google.script.run.withSuccessHandler(loadSheetName)
        .withFailureHandler(showError).getSheetName();
    poll();
    }, interval);
};

function loadSheetName(sheetName) {
   alert(sheetName);
   }

function getSheetName() {
   google.script.run.withSuccessHandler(loadSheetName)
        .withFailureHandler(showError).getSheetName();
   }

在您的 .gs 代码中

function getSheetName() {
var sheetName =         SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
return sheetName;
}

【讨论】:

  • 这太棒了,让我知道我到底在寻找什么。然而。它有一些语法错误,因此这里是我从上面更正的代码:$(function(){ poll(); }); function poll(interval){ interval = interval || 2000; setTimeout(function(){ google.script.run .withSuccessHandler(loadSheetName) .withFailureHandler(showError) .getSheetName(); poll(); }, interval); }; function loadSheetName(sheetName) { alert(sheetName); } function showError(error) { alert(error); }
  • @LJay 此代码早在 2016 年就可以使用。如果自那以后有更改,请将当前答案附加到您的代码中,或者在当前 api 调用中添加单独的答案
【解决方案2】:

除了您链接的文档中的事件之外,没有提供任何 API 来触发用户事件。

我只能想到一种解决方法:如果您的应用程序涉及自定义 UI 元素,特别是侧边栏或对话框,那么您可以轮询活动单元格,并从中确定活动工作表。

请参阅How to poll a Google Doc from an add-on 了解开始。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    相关资源
    最近更新 更多