在HTML中调用Google Apps脚本功能 [英] Call a Google Apps Script function in HTML

查看:115
本文介绍了在HTML中调用Google Apps脚本功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指导一个运动队并为它建立一个网站。我想添加一个按钮到一个管理页面,我可以点击快速发送一封电子邮件给团队中的每个人。这封电子邮件会显示如下内容:今天的日程安排已经更改,请访问网站以获取更多信息。



我相信这可以通过分发列表Outlook或其他内容,但我想使用并更好地理解Google Apps脚本。



我有一个带有电子邮件地址和简单脚本功能的Google电子表格发送电子邮件。



当我点击在脚本编辑器中运行它时,这很好用,但是有一种方法可以通过一个按钮从外部网站调用它,一些JavaScript?

解决方案

您需要创建一个可以嵌入网站的Google UI对象 - 创建一个脚本,您将部署为一个Web应用程序。 (请参阅 Google Apps脚本 - Web应用程序。)该对象将包含一个按钮调用您现有的脚本函数发送电子邮件。



发布您的web应用程序以与您一样执行。针对您的具体情况,您还需要访问仅限于您的应用。这将意味着您创建的用户界面Blob不会对普通大众有效。这是非常不完整的,但产生一个单一的按钮,将触发你的脚本的blob:

pre $函数doGet(){
var app = UiApp.createApplication();

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

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

返回应用;
}

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

//调用你的库函数,例如
TeamSpreadsheet.sendScheduleChanged();

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

label.setVisible(true);

app.close();
返回应用程序;
}

您的网络应用程序需要访问您现有的脚本,我认为嵌入在您的电子表格中。这是通过首先保存现有脚本的一个版本(文件 - 管理版本),然后将其作为库添加到新的Web应用程序脚本(资源 - 管理库)来完成的。阅读有关图书馆的更多信息此处。由于您的网络应用程序会像您一样运行,因此您可以保留电子表格的私密性。



注意事项



库中的实用程序脚本需要以可以在工作表外工作的方式打开电子表格;请使用 SpreadsheetApp.openById(),而不要使用SpreadsheetApp.getActiveSpreadsheet()。不幸的是,openById在你使用它打开主电子表格时会出现这种情况,所以你需要这样的东西:

  var ss =空值; 
try {
//这适用于在主电子表格中运行的脚本
ss = SpreadsheetApp.getActiveSpreadsheet();
} catch(err){
try {
//这适用于web应用
ss = SpreadsheetApp.openById(SPREADSHEET ID);
} catch(err){
Logger.log(无法打开源电子表格。);
//那么,那么没什么意义,是吗?
return;
}
}
SpreadsheetApp.setActiveSpreadsheet(ss);
...

实际上,请注意任何依赖于调用来从活动对象。你可能需要做一些体操来解决它们。



我发现调试这种安排是很痛苦的,因为应用程序脚本调试器经常报告服务器错误,当您尝试追踪库例程。



我希望能帮助您开始!


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.

I have a Google Spreadsheet with the email addresses and a simple script function which sends the email.

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?

解决方案

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. (Refer to Google Apps Script - Web Apps.) That object will contain a button that will invoke your existing script function to send the email.

Publish your web app to execute as you. 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. This is very incomplete, but produces a blob with a single button that will trigger your script:

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). 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(). Unfortunately, openById pukes when you use it to open the host spreadsheet, so you'll want something like this:

  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.

I hope that helps gets you started!

这篇关于在HTML中调用Google Apps脚本功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆