简体   繁体   English

从网站/本地主机运行应用程序脚本功能

[英]Run app script function from website/localhost

I'm really need help to running my function in google app script from my website/localhost.我真的需要帮助才能从我的网站/本地主机在 google 应用程序脚本中运行我的函数。

Already search on google, and the result must use google.script.run .已经在 google 上搜索,结果必须使用google.script.run When I'm trying it, I found the error that google is not function.当我尝试它时,我发现了google不起作用的错误。

This ini my .gs code in app script:这是我在应用程序脚本中的.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 );
}

I read thorough Google's App Script.我通读了 Google 的 App Script。 App Script is fundamentally built to provide a service to developers for extending google apps (spreadsheet, calendar, gmail, doc,...) and they are By Design expected to be located and run from google cloud servers. App Script 的根本目的是为开发人员提供扩展 google 应用程序(电子表格、日历、gmail、doc 等)的服务,并且按照设计,它们预计将在 google 云服务器上定位和运行。

So, You can not run them on your website/localhost, because GS scripts are executed server-side(google servers).所以,你不能在你的网站/本地主机上运行它们,因为 GS 脚本是在服务器端(谷歌服务器)执行的。

The only suggested way to do this, is to make an htmlService app and use ajax from the frontend .唯一建议的方法是制作一个 htmlService 应用程序并从前端使用 ajax Which I think is not your case.我认为这不是你的情况。

Probably the best and easiest thing you can do.可能是你能做的最好和最简单的事情。

Use Local Tunnel https://localtunnel.github.io/www/ This way your localhost can be accessed from any computer or a Google Server使用本地隧道https://localtunnel.github.io/www/这样可以从任何计算机或 Google 服务器访问您的本地主机

Localtunnel allows you to easily share a web service on your local development machine without messing with DNS and firewall settings. Localtunnel 允许您在本地开发机器上轻松共享 Web 服务,而不会弄乱 DNS 和防火墙设置。

Change the baseURL to the URL provided by the local tunnel and it will work like a charm.将 baseURL 更改为本地隧道提供的 URL,它将像魅力一样工作。

To run a Google Apps Script from an external web page or service you could use the Google Apps Script REST API (emphasis mine):要从外部网页或服务运行 Google Apps 脚本,您可以使用Google Apps Script REST API (重点是我的):

Google Apps Script API Google Apps 脚本 API

The Google Apps Script API replaces and extends the Apps Script Execution API. Google Apps Script API 取代并扩展了 Apps Script Execution API。 The Apps Script API allows your apps to perform operations that previously could only be done in the Apps Script editor. Apps Script API 允许您的应用执行以前只能在 Apps 脚本编辑器中完成的操作。 With the API, your apps can do the following programmatically:借助 API,您的应用可以通过编程方式执行以下操作:

  • Create and modify Apps Script projects.创建和修改 Apps 脚本项目。
  • Deploy projects for use as web apps, add-ons, or executables.部署项目以用作 Web 应用程序、附加组件或可执行文件。
  • Monitor script use and metrics.监控脚本使用和指标。
  • Execute Apps Script functions remotely.远程执行 Apps 脚本功能。

The above link include several examples for a wide range of languages including but no limited to JavaScript, PHP and Python.以上链接包括多种语言的几个示例,包括但不限于 JavaScript、PHP 和 Python。

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

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