在复制的 Google Sheets 中,onOpen 函数未能触发。

huangapple go评论49阅读模式
英文:

onOpen function not launching in the duplicated Google Sheets

问题

我的onOpen()函数在Google Sheets的任务栏中添加了2个按钮。它曾经完美地运行,即使在复制的表格中,按钮也会出现。但突然间,在复制的文件中不再出现。

我目前所做的是每次复制表格时都添加一个触发器来打开它。但我必须运行onOpen()函数,否则它不起作用。

我的函数非常简单和基本:

function onOpen() {
 
  var ui = SpreadsheetApp.getUi();

  ui.createMenu('Present Data')
    .addItem("Generate Report",'report')
    .addToUi();

  ui.createMenu('Reset')
    .addItem("Meta","resetmetaFunction")
    .addItem("Snapchat","resetsnapFunction")  
    .addItem("TikTok","resettiktokFunction")
    .addItem("Twitter","resettwitterFunction")
    .addItem("Youtube","resetyoutubeFunction")  
    .addItem("Google","resetgoogleFunction")
    .addItem("LinkedIn","resetlinkedinFunction")  
    .addItem("Programmatic","resetprogrammaticFunction")        
    .addItem("Clear All","resetAll")      
    .addToUi();
}

可能导致脚本在启动时停止运行的原因是什么?

英文:

My onOpen() function adds 2 buttons in the taskbar of Google Sheets. It used to work perfectly and the buttons appeared even in duplicated sheets. But suddenly it stopped appearing in duplicated files.

What I am currently doing is adding a trigger every time I duplicate the sheet to open it. But I have to run the onOpen() function otherwise it doesn't work.

My function is very simple and basic

function onOpen() {
 
  var ui = SpreadsheetApp.getUi();

  ui.createMenu('Present Data')
    .addItem("Generate Report",'report')
    .addToUi();

  ui.createMenu('Reset')
    .addItem("Meta","resetmetaFunction")
    .addItem("Snapchat","resetsnapFunction")  
    .addItem("TikTok","resettiktokFunction")
    .addItem("Twitter","resettwitterFunction")
    .addItem("Youtube","resetyoutubeFunction")  
    .addItem("Google","resetgoogleFunction")
    .addItem("LinkedIn","resetlinkedinFunction")  
    .addItem("Programmatic","resetprogrammaticFunction")        
    .addItem("Clear All","resetAll")      
    .addToUi();
}

What could possibly cause the script to stop running on startup ?

答案1

得分: 1

老实说,当你只是复制表格时,应该仍然运行onOpen()的行为,并允许你看到菜单并使用它。然而,当点击菜单时,它应该请求访问Google Drive和运行脚本所需的其他权限。你可以按照这里的步骤报告这个问题。

注意:这考虑到你在Google表格中有编辑权限,并且你可以通过DriveApp.getFileById()调用Drive文件。

因为脚本尚未获得授权,也没有触发身份验证,你可以尝试以下解决方法。

当你第一次运行脚本时,正常的流程是你应该会得到一个OAuth屏幕,授权脚本所需的所有范围。为了强制执行这个,你有3个选项,首先尝试手动运行脚本以强制脚本的身份验证。使用Run

第二个选项是通过编程方式创建一个可安装的触发器。类似于:

function createSpreadsheetOpenTrigger() {
  const ss = SpreadsheetApp.getActive();
  ScriptApp.newTrigger('onOpen')
      .forSpreadsheet(ss)
      .onOpen()
      .create();
}

在这之前:

function onOpen() {
 
  var ui = SpreadsheetApp.getUi();

  ui.createMenu('Present Data')
    .addItem("Generate Report",'report')
    .addToUi();

  ui.createMenu('Reset')
    .addItem("Meta","resetmetaFunction")
    .addItem("Snapchat","resetsnapFunction")  
    .addItem("TikTok","resettiktokFunction")
    .addItem("Twitter","resettwitterFunction")
    .addItem("Youtube","resetyoutubeFunction")  
    .addItem("Google","resetgoogleFunction")
    .addItem("LinkedIn","resetlinkedinFunction")  
    .addItem("Programmatic","resetprogrammaticFunction")        
    .addItem("Clear All","resetAll")      
    .addToUi();
}

你最后可以手动在Apps脚本清单中添加权限范围。按照以下步骤:

  1. 打开脚本项目。
  2. 在左边,点击“项目设置”(齿轮图标)。
  3. 选择在编辑器中显示“appsscript.json”清单文件的复选框。
  4. 在左边,点击“编辑器”代码。
  5. 在左边,点击“appsscript.json”文件。
  6. 找到顶级字段标记为“oauthScopes”。如果不存在,你可以添加它。
  7. oauthScopes字段指定一个字符串数组。为了设置你的项目使用的权限,将此数组的内容替换为你希望它使用的权限。
        "oauthScopes": [
          "https://www.googleapis.com/auth/drive.readonly",
          "https://www.googleapis.com/auth/drive"
        ]

看起来会是这样的:

英文:

To be honest the behavior when just duplicating the sheet, should still run the onOpen(), and allow you to see the menu and allow you to see it. However, when clicking on the menu it should request the permissions to access Google Drive and anything else required to run the script. You can report that issue by following the steps here

Note: This is taking into consideration that you have editing permissions in the Google Sheet, and you have access to the Drive file called by DriveApp.getFileById().

Since the script is not authorized yet, and is not triggering the authentication You can try these workarounds for the moment.

The normal flow when you run the script for the first time, you should get an OAuth screen to authorize all the scopes needed by the script. To force this, you have 3 options, first try to run the script manually to force the Authentication of the Script. Using Run.

在复制的 Google Sheets 中,onOpen 函数未能触发。

在复制的 Google Sheets 中,onOpen 函数未能触发。


The second option is to programmatically create an installable trigger. Something like:

function createSpreadsheetOpenTrigger() {
  const ss = SpreadsheetApp.getActive();
  ScriptApp.newTrigger('onOpen')
      .forSpreadsheet(ss)
      .onOpen()
      .create();
}

Before the:

function onOpen() {
 
  var ui = SpreadsheetApp.getUi();

  ui.createMenu('Present Data')
    .addItem("Generate Report",'report')
    .addToUi();

  ui.createMenu('Reset')
    .addItem("Meta","resetmetaFunction")
    .addItem("Snapchat","resetsnapFunction")  
    .addItem("TikTok","resettiktokFunction")
    .addItem("Twitter","resettwitterFunction")
    .addItem("Youtube","resetyoutubeFunction")  
    .addItem("Google","resetgoogleFunction")
    .addItem("LinkedIn","resetlinkedinFunction")  
    .addItem("Programmatic","resetprogrammaticFunction")        
    .addItem("Clear All","resetAll")      
    .addToUi();
}

Last option that you can do is to [manually add the scope](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes
s) on the Apps script Manifest by following the steps:

  1. Open the script project.
  2. At the left, click Project Settings (gear icon).
  3. Select the Show "appsscript.json" manifest file in editor checkbox.
  4. At the left, click Editor code.
  5. At the left, click the appsscript.json file.
  6. Locate the top-level field labeled oauthScopes. If it's not present, you can add it.
  7. The oauthScopes field specifies an array of strings. To set the scopes your project uses, replace the contents of this array with the scopes you want it to use.
        "oauthScopes": [
          "https://www.googleapis.com/auth/drive.readonly",
          "https://www.googleapis.com/auth/drivel"
        ]

And it will look like this:

在复制的 Google Sheets 中,onOpen 函数未能触发。

huangapple
  • 本文由 发表于 2023年3月10日 01:16:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687942.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定