【问题标题】:Run app script function from website/localhost从网站/本地主机运行应用程序脚本功能
【发布时间】:2017-07-17 06:36:28
【问题描述】:

我真的需要帮助才能从我的网站/本地主机在 google app 脚本中运行我的函数。

已经在google上搜索,结果必须使用google.script.run。我在尝试的时候发现google is not function的错误。

此 ini 我的 .gs 应用脚本中的代码:

/**
*  This script demonstrates the use of objDB using a spreadsheet as a database
*
*  objDB is a Google Script Library that makes it easy to work with data stored in a spreadsheet.
*  Using the same functions, you can also work with data from a JDBC-connected database.
*  See http://googlescripts.harryonline.net/objdb for full documentation
*  
*  This demo uses the spreadsheet template from the tutorial on the Google Apps Script
*  https://developers.google.com/apps-script/storing_data_spreadsheets#reading
*  
*/

/**
*  To restore the data from the original spreadsheet, run getSpreadsheet()
*  Try out the sample function, and create your own.
*  See the results in the log, as well as on the spreadheet itself
*  You can view the log file using View - Logs, or Alt-Enter
*  Alternatively, put breakpoints at the Logger.log statements and use Debug instead of Run

/**
*  Create a copy of the tutorial spreadsheet and stores the ID of the newly created spreadsheet
*  in Usersettings, so you can continue using this.
*  Restore data from original tutorial spreadsheet
*/

function getSpreadsheet()
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Tutorial Data');
  sheet.clear();
  var tutorialID = '0Aq4s9w_HxMs7dFNtWlh2MHRWZzEtbk5LRW5hTVR1Y1E';
  var tutorialDataRange = SpreadsheetApp.openById(tutorialID).getSheets()[0].getDataRange();
  var range = sheet.getRange(1,1,tutorialDataRange.getNumRows(), tutorialDataRange.getNumColumns());
  range.setValues(tutorialDataRange.getValues());
}

/**
*  Sample: get all data from Engineering employees
*/

function getEngineers()
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ssDB = objDB.open( ss.getId());
  var rows = objDB.getRows(ssDB, 'Tutorial Data', [], {Department:'Engineering'});
  Logger.log( rows );
}

/**
*  Sample: get John's phone number
*  Note that non-alphanumeric characters are stripped from column names: Phone Number becomes PhoneNumber
*/

function getPhone()
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ssDB = objDB.open( ss.getId());
  var rows = objDB.getRows(ssDB, 'Tutorial Data', ['PhoneNumber'], {FirstName:'John'});
  Logger.log( rows );
}

/**
*  Delete staff with id's 1342 and 1234 
*/

function deleteStaff()
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ssDB = objDB.open( ss.getId());
  var rowCount = objDB.deleteRow(ssDB, 'Tutorial Data', {EmployeeId:[1342,1234]});
  Logger.log( rowCount );
}

/**
*  Update: staff 3512 goes to marketing
*/

function updateStaff()
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ssDB = objDB.open( ss.getId());
  var rowCount = objDB.updateRow(ssDB, 'Tutorial Data', {Department:'Marketing'}, {EmployeeId:3512});
  Logger.log( rowCount );
}

/**
*  Add new employee
*/

function addStaff()
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ssDB = objDB.open( ss.getId());
  var rowCount = objDB.insertRow(ssDB, 'Tutorial Data', {FirstName:'Harry', LastName:'Potter', EmployeeId:4321, Department:'Magic',PhoneNumber:'(212) 123-4567'});
  Logger.log( rowCount );
}

【问题讨论】:

  • 您不应该在页面中包含任何其他 .js 文件吗?
  • @BehradKhodayar 我必须在我的网页中包含什么 javascript 文件?
  • 通常您应该在文档中包含第三方库才能使用它
  • 但是没有第三方库
  • 如果您想从非 Google 产品运行 Apps 脚本代码,您可以向 Apps 脚本项目中的 doGet()doPost() 函数发出 HTTPS GET 或 POST 请求。 doGet()doPost() 是保留的函数名称,由对 Apps Script Web App 的已发布 URL 的 GET 或 POST 请求触发。可以从 Apps 脚本将返回值发送回使用 Content Service 发出 GET 或 POST 请求的任何对象。请参阅以下 Stack Overfow 帖子:SO Post Call a custom GAS function from external URL

标签: javascript google-apps-script


【解决方案1】:

我通读了 Google 的 App Script。 App Script 旨在为开发人员提供用于扩展 google 应用程序(电子表格、日历、gmail、doc 等)的服务,并且它们按设计预计将在 google 云服务器上定位和运行.

因此,您不能在您的网站/本地主机上运行它们,因为 GS 脚本是在服务器端(谷歌服务器)执行的。

建议的唯一方法是is to make an htmlService app and use ajax from the frontend。我认为这不是你的情况。

【讨论】:

  • 还有另一种方法。见my answer
  • @Rubén 谢谢,但这里的案例不涉及 API 版本(正如我在回答末尾所建议的那样)。他试图将 .gs 文件上传到他的主机/本地主机 & ... 。这就是为什么我对错误的过程提出警告。
  • 不客气。看来我对这个问题的理解和你一样,你的建议和我的不一样:)
【解决方案2】:

要从外部网页或服务运行 Google Apps 脚本,您可以使用 Google Apps Script REST API(强调我的):

Google Apps 脚本 API

Google Apps Script API 取代并扩展了 Apps Script Execution API。 Apps 脚本 API 允许您的应用程序执行以前只能在 Apps 脚本编辑器中完成的操作。借助 API,您的应用可以以编程方式执行以下操作:

  • 创建和修改 Apps 脚本项目。
  • 部署项目以用作 Web 应用程序、插件或可执行文件。
  • 监控脚本使用和指标。
  • 远程执行 Apps 脚本功能。

以上链接包含多个示例,适用于各种语言,包括但不限于 JavaScript、PHP 和 Python。

【讨论】:

  • 问题询问如何在 from 而不是 on 本地主机上运行 Google Apps 脚本。
【解决方案3】:

这可能是你能做的最好和最简单的事情。

使用本地隧道https://localtunnel.github.io/www/ 这样就可以从任何计算机或 Google 服务器访问您的本地主机

Localtunnel 允许您在本地开发机器上轻松共享 Web 服务,而不会弄乱 DNS 和防火墙设置。

将 baseURL 更改为本地隧道提供的 URL,它会像一个魅力一样工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 2023-04-11
    • 2012-06-09
    • 2016-09-16
    • 2016-11-30
    • 1970-01-01
    • 2019-06-29
    相关资源
    最近更新 更多