简体   繁体   English

如何使用 Google 表格脚本在打开事件中打开模态表单?

[英]How to use Google Sheets script to open a modal form on open event?

It only works if I call the modal by pressing from the custom menu, but if I put it in the event when opening it does not load it, but if it creates the menu that's why I don't know why it doesn't load the modal它只有在我通过从自定义菜单中按下来调用模态时才有效,但是如果我在打开它时将它放在事件中它不会加载它,但是如果它创建菜单这就是为什么我不知道它为什么不加载模态

 const spreadsheetId = 'your spreedsheet id'; function addMenuItem() { SpreadsheetApp.getUi().createMenu('My Functions').addItem('Show Dialog', 'showModal').addToUi(); } function showModal() { const userInterface = HtmlService.createHtmlOutputFromFile('modal'); SpreadsheetApp.getUi().showModelessDialog(userInterface, 'My modal'); } function inputData(data) { SpreadsheetApp.openById(spreadsheetId).getSheetByName('Sheet1').appendRow([data]); } function onOpen(e){ addMenuItem(); showModal(); }
 <,DOCTYPE html> <html> <head> <base target="_top"> </head> <body> <h1>Hello. and Welcome.</h1> <input id='my_input' placeholder='Insert your Data'><br> <button id='my_button'>Submit</button> <script> document,getElementById('my_button').addEventListener('click'. _ => { const data = document;getElementById('my_input').value. google.script.run;withSuccessHandler(displaySuccess);inputData(data). }). function displaySuccess() { google.script;host;close(); } }); </script> </body> </html>

You are currently using Simple Triggers您当前正在使用简单触发器

Simple Triggers Restrictions: 简单触发器限制:

They cannot access services that require authorization.他们无法访问需要授权的服务。 For example, a simple trigger cannot send an email because the Gmail service requires authorization, but a simple trigger can translate a phrase with the Language service, which is anonymous.例如,简单触发器无法发送 email,因为 Gmail 服务需要授权,但简单触发器可以使用语言服务翻译短语,这是匿名的。

  • You are using HTML Service therefore you need to use Installable Triggers since they can call services that require authorization.您正在使用 HTML 服务,因此您需要使用可安装触发器,因为它们可以调用需要授权的服务。 I tried adding onOpen() as a installable trigger and when I open the file, the modal was shown successfully.我尝试将 onOpen() 添加为可安装触发器,当我打开文件时,模态显示成功。

There are 2 ways on how to create an installable trigger:有两种创建可安装触发器的方法:

1. Managing triggers manually 1. 手动管理触发器

  • Open your Apps Script project.打开您的 Apps 脚本项目。
  • click Triggers alarm.单击触发警报。
  • At the bottom right, click Add Trigger.在右下角,单击添加触发器。
  • Select and configure the type of trigger you want to create. Select 并配置您要创建的触发器类型。
  • Click Save.单击保存。

Sample Configuration:示例配置:

在此处输入图像描述

2. Managing triggers programmatically 2. 以编程方式管理触发器

/**
 * Creates a trigger for when a spreadsheet opens.
 */
function createSpreadsheetOpenTrigger() {
  var ss = SpreadsheetApp.getActive();
  ScriptApp.newTrigger('myFunction')
      .forSpreadsheet(ss)
      .onOpen()
      .create();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MaterializeCss 使用 dblclick 事件打开 datepicker modal - MaterializeCss Use dblclick event to open datepicker modal 通过脚本编辑器创建的谷歌电子表格的自定义菜单调用谷歌表单并通过弹出/模态打开它 - Calling a Google Form through the Custom Menu of Google Spreadsheet created by the script editor and open it through a pop/modal 如何打开模态就像谷歌日历模式,在完整日历中的一天点击事件 - How to open modal just like google calendar modal, on day click event in full calendar 如何以编程方式为 Google Apps 脚本中的打开事件创建触发器 - How to create a trigger programmatically for an open event in Google Apps Script "如何在谷歌应用脚​​本函数中使用模态表单数据?" - How to use the modal form data in the google apps script functions? 脚本函数不会打开模态 - script function will not open modal 表单提交后如何打开模式 - How to open a modal after form submission 如何创建 Google Sheets Macro/Apps 脚本来打开新添加的文件并将数据复制到 Master? - How to create Google Sheets Macro/Apps Script to Open Newly Added File and Copy Data to Master? 当我的模式已经打开时阻止“显示”事件(脚本重新加载) - Block the "show" event when my modal is already open (the script reloads) 如何使用jquery .load打开模式 - How to use jquery .load to open modal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM