admin管理员组文章数量:1401499
I'm really need help to running my function in google app script from my website/localhost.
Already search on google, and the result must use google.script.run
. When I'm trying it, I found the error that google
is not function.
This ini my .gs
code in app script:
/**
* 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 for full documentation
*
* This demo uses the spreadsheet template from the tutorial on the Google Apps Script
*
*
*/
/**
* 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 bees 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'm really need help to running my function in google app script from my website/localhost.
Already search on google, and the result must use google.script.run
. When I'm trying it, I found the error that google
is not function.
This ini my .gs
code in app script:
/**
* 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/objdb for full documentation
*
* This demo uses the spreadsheet template from the tutorial on the Google Apps Script
* https://developers.google./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 bees 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 );
}
Share
Improve this question
edited Feb 26, 2017 at 18:29
Emran
1881 silver badge13 bronze badges
asked Feb 26, 2017 at 18:21
DeVoresyahDeVoresyah
861 gold badge1 silver badge9 bronze badges
6
- 1 Shouldn't you include any other .js file in your page? – Behrad Khodayar Commented Feb 26, 2017 at 18:46
- @BehradKhodayar what javascript file i must include in my web page? – DeVoresyah Commented Feb 26, 2017 at 19:10
- Usually you should include the third-party library in your document in order to be able to use it – Behrad Khodayar Commented Feb 26, 2017 at 19:27
- but there is not third-party library – DeVoresyah Commented Feb 26, 2017 at 19:32
-
2
If you want to run Apps Script code from a non Google product, you can make an HTTPS GET or POST request to either the
doGet()
ordoPost()
functions in an Apps Script project.doGet()
anddoPost()
are reserved function names that are triggered by a GET or POST request to the published URL of the Apps Script Web App. A return value can be sent from Apps Script back to whatever made the GET or POST request with Content Service. See the following Stack Overfow post: SO Post Call a custom GAS function from external URL – Alan Wells Commented Feb 26, 2017 at 20:04
3 Answers
Reset to default 3Probably 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 puter or a Google Server
Localtunnel allows you to easily share a web service on your local development machine without messing with DNS and firewall settings.
Change the baseURL to the URL provided by the local tunnel and it will work like a charm.
I read thorough Google's 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.
So, You can not run them on your website/localhost, because GS scripts are executed server-side(google servers).
The only suggested way to do this, is to make an htmlService app and use ajax from the frontend. Which I think is not your case.
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 Script API
The Google Apps Script API replaces and extends the 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. With the API, your apps can do the following programmatically:
- Create and modify Apps Script projects.
- Deploy projects for use as web apps, add-ons, or executables.
- Monitor script use and metrics.
- Execute Apps Script functions remotely.
The above link include several examples for a wide range of languages including but no limited to JavaScript, PHP and Python.
本文标签: javascriptRun app script function from websitelocalhostStack Overflow
版权声明:本文标题:javascript - Run app script function from websitelocalhost - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744226053a2596093.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论