简体   繁体   English

以HTML格式调用Google Apps脚本功能

[英]Call a Google Apps Script function in HTML

I coach a sports team and set up a website for it. 我指导一个运动队并为它建立一个网站。 I would like to add a button to an admin page which I can click to quickly send an email to everyone on the team. 我想在管理页面添加一个按钮,我可以点击该按钮快速向团队中的每个人发送电子邮件。 This email will say something like: "Today's schedule has changed, please visit the website for more info." 这封电子邮件会说:“今天的时间表已经改变,请访问网站了解更多信息。”

I am sure this could be achieved easier though a distribution list in outlook or something, but I want to use, and get a better understanding of, Google Apps Script. 我相信通过Outlook中的分发列表或其他东西可以更容易地实现这一点,但我想使用并更好地理解Google Apps脚本。

I have a Google Spreadsheet with the email addresses and a simple script function which sends the email. 我有一个Google电子表格,其中包含电子邮件地址和一个发送电子邮件的简单脚本功能。

This works great when I click to run it inside the script editor, but is there a way to call this from an external website with a button and some JavaScript? 当我点击在脚本编辑器中运行它时,这很有效,但是有没有办法从一个带有按钮和一些JavaScript的外部网站调用它?

You need to create a Google UI object that you can embed in your site - you'll do this by creating a Script that you will deploy as a web app. 您需要创建一个可以嵌入网站的Google UI对象 - 您可以通过创建一个将部署为Web应用程序的脚本来完成此操作。 (Refer to Google Apps Script - Web Apps .) That object will contain a button that will invoke your existing script function to send the email. (请参阅Google Apps脚本 - 网络应用 。)该对象将包含一个按钮,该按钮将调用您现有的脚本功能来发送电子邮件。

Publish your web app to execute as you. 发布您的Web应用程序以便像您一样执行。 For your specific situation, you will also want to have access to the app limited to you. 根据您的具体情况,您还可以访问仅限于您的应用。 That will mean that the UI Blob you create will not function for the general public. 这意味着您创建的UI Blob将无法为普通公众运行。 This is very incomplete, but produces a blob with a single button that will trigger your script: 这是非常不完整的,但会产生一个带有单个按钮的blob,它会触发您的脚本:

function doGet() {
  var app = UiApp.createApplication();

  var button = app.createButton('Send Schedule Change Email');
  app.add(button);

  var handler = app.createServerHandler('myClickHandler');
  button.addClickHandler(handler);

  return app;
}

function myClickHandler(e) {
  var app = UiApp.getActiveApplication();

  // Call your library function, e.g.
  TeamSpreadsheet.sendScheduleChanged();

  var label = app.createLabel('Message Sent')
                 .setId('statusLabel')
                 .setVisible(false);

  label.setVisible(true);

  app.close();
  return app;
}

Your web app will need to have access to your existing script, which I assume is embedded in your spreadsheet. 您的网络应用需要访问您现有的脚本,我认为该脚本已嵌入您的电子表格中。 This is accomplished by first saving a version of your existing script (File - Manage Versions), then adding it as a library to your new web app script (Resources - Manage Libraries). 这是通过首先保存现有脚本的一个版本(文件 - 管理版本),然后将其作为库添加到新的Web应用程序脚本(资源 - 管理库)来实现的。 Read more about libraries here . 在这里阅读更多关于图书馆 Since your web app will run as you, you can keep your spreadsheet private. 由于您的网络应用会像您一样运行,因此您可以将电子表格保密。

Caveats 注意事项

The utility script in your library will need to open your spreadsheet in a way that will work from outside of the sheet; 库中的实用程序脚本需要以从工作表外部开始工作的方式打开电子表格; use SpreadsheetApp.openById() , rather than SpreadsheetApp.getActiveSpreadsheet(). 使用SpreadsheetApp.openById() ,而不是SpreadsheetApp.getActiveSpreadsheet()。 Unfortunately, openById pukes when you use it to open the host spreadsheet, so you'll want something like this: 不幸的是,当你使用openKyId打开主机电子表格时,它会让你想要这样的东西:

  var ss = null;
  try {
    // This works for scripts running in the host spreadsheet
    ss = SpreadsheetApp.getActiveSpreadsheet();
  } catch(err) {
    try {
      // This works for web apps
      ss = SpreadsheetApp.openById("SPREADSHEET ID");
    } catch(err) {
      Logger.log("Could not open source spreadsheet.");
      // Well, then, not much sense carrying on, is there?
      return;
    }
  }
  SpreadsheetApp.setActiveSpreadsheet(ss);
  ...

In fact, watch for any reliance on calls to get info from "active" objects. 实际上,请注意依赖于从“活动”对象获取信息的调用。 You may need to do some gymnastics to get around them. 你可能需要做一些体操来绕过它们。

I've found debugging this type of arrangement to be painful, since the app-script debugger often reports "Server Errors" when you try to trace into a library routine. 我发现调试这种类型的安排很痛苦,因为当你试图追踪到一个库例程时,app-script调试器经常报告“服务器错误”。

I hope that helps gets you started! 我希望这有助于你开始!

As a complement to Mogsdad's answer (that was quite complete and interesting) I'd say that your case could be a bit simpler since you have already a working script. 作为Mogsdad答案的补充(这是非常完整和有趣的)我会说你的案例可能会更简单,因为你已经有了一个工作脚本。

Take your script and add a doGet() function like in Mogsdad example, define a handler on the button that will call your existing function you wrote to send mails, in this function replace the getActiveSpreadsheet() by SpreadsheetApp.OpenById("the ID of the SS") and the getActiveSheet() by OpenByName() and you're done with the modifications. 拿你的脚本并添加一个doGet()函数,就像在Mogsdad示例中一样,在按钮上定义一个处理程序,它将调用你编写的现有函数来发送邮件,在这个函数中用SpreadsheetApp.OpenById("the ID of the SS")替换getActiveSpreadsheet() SpreadsheetApp.OpenById("the ID of the SS")OpenByName()getActiveSheet() OpenByName() ,你完成了修改。

After that follow Mogsdad instructions : deploy the script as a webapp running as you and use the provided url to access it from a link on your site. 然后遵循Mogsdad说明:将脚本部署为运行的webapp,并使用提供的URL从您站点上的链接访问它。

If you want not to take any risk, do all these changes on a copy of your original spreadsheet so you always keep a working model at hand. 如果您不想冒任何风险,请在原始电子表格的副本上进行所有这些更改,以便始终保持工作模式。 If you want more accurate advice (or if you meet some difficulties) feel free to show your existing script to get some help with the modifications. 如果您想要更准确的建议(或者如果您遇到一些困难),请随时显示您现有的脚本以获得有关修改的一些帮助。

PS : please consider this as a simple comment, written in an answer for the comfort of formating PS:请将此视为一个简单的评论,写在一个答案中,以便舒适地形成

Just publish your script as web application and upon a button click, excecute: 只需将您的脚本发布为Web应用程序,然后单击按钮,即可:

window.open("https://script.google.com/macros/s/<id>/exec");

Don't know if this was possible a year ago. 不知道一年前这是否可能。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM