onOpen 不是 运行 除了所有者以外的其他用户。如何解决这个问题? Google 脚本 GAS

onOpen is not running to other users except to the owner. How to fix this? Google Script GAS

刷新共享电子表格后,onOpen 不是 运行 其他也有访问权限的用户。

但所有者能够运行代码和脚本。

Code.gs

function onOpen(e) {
    test();
  SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp, or FormApp.
      .createMenu('Custom Menu')
      .addItem('First item', 'menuItem1')
      .addToUi();
    SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp, or FormApp.
        .createMenu('Custom Menu').addItem('Test', 'test').addToUi();
};

function test() {
    var ui = SpreadsheetApp.getUi(); // Same variations.
    var result = ui.prompt('Spreadsheet Restriction', 'Enter password:', ui.ButtonSet.OK);
    // Process the user's response.
    var button = result.getSelectedButton();
    var text = result.getResponseText();
    if (button == ui.Button.OK) {
        // User clicked "OK".
    } else if (button == ui.Button.CLOSE) {
        // User clicked X in the title bar.
        ui.alert('Spreadsheet is protected.');
        test();
    }
};
function myFunction() {

}

已将访问电子表格和脚本共享给其他用户,但他们仍然看不到它正在运行。 感谢你们的帮助...谢谢!

  1. 您的 Sheet/script 的用户必须 对表格文档具有编辑权限才能执行 onOpen() 触发器.来自文档:

    They do not run if a file is opened in read-only (view or comment) mode.

  2. 为了在 onOpen() 函数中使用 prompt() 方法,您 必须 使用可安装的触发器。此外,触发器 必须 由将要使用它的用户设置。 UI class such as showModalDialog().

    的其他函数也是如此

执行此严格规则很可能是为了保护最终用户免遭潜在诈骗。如果您有兴趣使用密码保护您的表格文档,我建议您查看其他解决方案,例如 this one

参考