Add to Google Sheets with a Google Apps Script Web App - html

I am just getting started with Google Apps Script and I want to be able to add to a Google Sheet through a web app, but I'm having issues. This is what I have so far:
function addRow(input) {
var sheet = SpreadsheetApp.openByUrl('https://spreadsheeturl');
sheet.appendRow([input]);
}
function doGet(e) {
var params = JSON.stringify(e);
addRow(params);
return HtmlService.createHtmlOutput(params);
}
When I type in the URL they give me with the parameters I want to add to the spreadsheet it doesn't add anything to the spreadsheet, but if I don't pass any parameters it adds it to the spreadsheet.
For example, https://script.google.com/.../exec adds {"parameter":{},"contextPath":"","contentLength":-1,"queryString":"","parameters":{}} to the spreadsheet, but https://script.google.com/.../exec?user=jsmith doesn't add anything to the spreadsheet.
Reading the documentation I can see something about URL parameters and event objects, but they give no further information. What's the problem and how can I fix it?
Thanks,
Tomi

I think that your script works. In your script,
When https://script.google.com/.../exec is requested, {"parameter":{},"contextPath":"","contentLength":-1,"queryString":"","parameters":{}} is added to the last row at the 1st sheet of the Spreadsheet.
When https://script.google.com/.../exec?user=jsmith is requested, {"parameter":{"user":"jsmith"},"contextPath":"","contentLength":-1,"queryString":"user=jsmith","parameters":{"user":["jsmith"]}} is added to the last row at the 1st sheet of the Spreadsheet.
So can you confirm the following points on your script editor again?
At Publish -> Deploy as web app, redeploy as a new version for Project version:.
When the scripts for Web Apps was modified, the modified script is required to be redeployed as a new version. By this, the modified script is reflected to Web Apps.
At Publish -> Deploy as web app, it confirms whether Execute the app as: and Who has access to the app: are "me" and "Anyone, even anonymous", respectively.
If I misunderstand your question, I'm sorry.

Related

When creating copy of a spreadsheet that has bundled script using drive api, script behind doesn't work [duplicate]

I have a GSheet that is owned by a service account, and I would like to add an “on edit” trigger to receive real-time edit notifications.
So far I've tried/looked at:
Simply adding the onEdit function to the bound script, however Google doesn’t allow Apps Script to be run on the script (issue is discussed more here):
Google Apps Script: The script cannot be run because it is owned by a service account. Please copy or transfer the project to a valid account before running.
Drive API push notifications - but that doesn’t give the per cell detail required, and regardless neither does it give a response faster enough; can be up to 3 mins according this SO post.
Changing the ownership of the GSheet - however other requirements require the GSheet to remain belonging to the service account.
Using the Script API - but it isn’t allowed to create triggers, and anyway service accounts can’t use the Script API.
Any other ideas, or is the fact Google simply doesn’t allow Apps Script to run under the service account a show stopper?”
I believe your goal is as follows.
You have a Google Spreadsheet created by the service account.
You want to use the OnEdit trigger on this Spreadsheet.
Updated on January 25, 2023:
I confirmed that in the current stage, your goal can be directly achieved using the installable OnEdit trigger. The sample flow is as follows.
Flow
1. Create a new Google Spreadsheet by service account.
Please create a new Google Spreadsheet by the service account. And, please copy the Spreadsheet ID. In this case, the owner of the Spreadsheet is the service account.
And, please share this Spreadsheet with your Google account as a writer. This is an important point.
2. Create a standalone script by your account.
Please create a new standalone script with your account. In this case, the owner of the standalone script is your account.
And, please copy and paste the following script to the script editor. And, please set the Spreadsheet ID of Spreadsheet of the service account to spreadsheetId and save the script.
function installTrigger() {
const spreadsheetId = "###"; // Please set the spreadsheet ID of your Spreadsheet.
const ss = SpreadsheetApp.openById(spreadsheetId);
ScriptApp.newTrigger("installedOnEdit").forSpreadsheet(ss).onEdit().create();
}
// In this pattern, please set your script in this function.
function installedOnEdit(e) {
e.range.setValue(JSON.stringify(e));
}
3. Install the OnEdit trigger.
In order to install the OnEdit trigger, please run installTrigger(). By this, the OnEdit trigger is installed to the function installedOnEdit. By this, when a cell of the above Spreadsheet is edited, installedOnEdit is run.
4. Testing.
Please edit the cell in the Spreadsheet of the service account. By this, the script of your standalone script is run, and you can see the event object in the edited cell.
References:
Installable Triggers
Old post:
Issue and workaround:
In the current stage, unfortunately, when the owner of Google Spreadsheet is the service account. The Google Apps Script cannot be run. By this, even when onEdit function is put to the script editor, this script cannot be run. It seems that this is the current specification of the Google side. Unfortunately, in the current stage, your goal cannot be directly achieved. I think that this is the current answer to your question.
But, I thought that a workaround might be able to be proposed. Fortunately, the built-in functions of the Spreadsheet can be also used in the Spreadsheet created by the service account. I thought that this might be able to be used as a workaround.
When I saw your question, I remembered the following my posts.
Automatic Recalculation of Custom Function on Spreadsheet Part 1
Letting Users Running Google Apps Script on Google Spreadsheet without both Authorizing Scopes and Showing Script
I thought that when these posts are used, a workaround might be able to be proposed. In this answer, I would like to propose a workaround for achieving your goal. So, please think of this as the pseudo OnEdit trigger.
The flow of this workaround is as follows.
Prepare Web Apps.
This Web Apps is deployed at the Google Apps Script project created by an account that the script can be run. Please be careful about this.
When the pseudo OnEdit trigger is used in "Sheet1", put IMPORTXML to another sheet (For example, it's "Sheet2".). IMPORTXML requests to the Web Apps.
This Spreadsheet is the Spreadsheet created by the service account.
When the cells in "Sheet1" are edited, the formula in "Sheet2" is run.
By this flow, when the cells in "Sheet1" are edited, the Google Apps Script of Web Apps can be run. This is the pseudo OnEdit trigger as this workaround.
Usage:
1. Create Google Apps Script for Web Apps.
In order to use Web Apps, please create Google Apps Script project by the account that the script can be run. In this case, as a sample situation, please create Google Apps Script project by your account instead of the service account.
2. Sample script:
Please copy and paste the following script to the script editor of the created Google Apps Script project.
function doGet(e) {
// do something
// Please set the script you want to use.
return ContentService.createTextOutput(`<result>Edited at ${new Date().toISOString()}</result>`).setMimeType(ContentService.MimeType.XML);
}
3. Deploy Web Apps.
The detailed information can be seen at the official document.
On the script editor, at the top right of the script editor, please click "click Deploy" -> "New deployment".
Please click "Select type" -> "Web App".
Please input the information about the Web App in the fields under "Deployment configuration".
Please select "Me" for "Execute as".
This is the importance of this workaround.
Please select "Anyone" for "Who has access".
In your situation, I thought that this setting might be suitable.
Please click "Deploy" button.
Copy the URL of the Web App. It's like https://script.google.com/macros/s/###/exec.
When you modified the Google Apps Script, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful this.
You can see the detail of this in the report of "Redeploying Web Apps without Changing URL of Web Apps for new IDE".
3. Testing.
Please prepare 2 sheets in Google Spreadsheet created by the service account. Those are "Sheet1" and "Sheet2".
Please put the following formula to "A1" of "Sheet2". In this case, please use your Web Apps URL. And, please confirm that the value is returned from the deployed Web Apps. If this cannot be done, please check the above flow again.
=IMPORTXML("https://script.google.com/macros/s/###/exec?values="&TEXTJOIN(",",TRUE,Sheet1!A:Z),"/result")
This formula is a sample formula. So, please modify the range and formula for your actual situation.
Please edit the cell on "Sheet1". When you use the above formula, please edit the cells in "A:Z". By this, the cell "A1" of "Sheet2" is refreshed, and by this refresh, Google Apps Script of Web Apps is run. This is the pseudo OnEdit trigger as this workaround.
Note:
When you modified the Google Apps Script, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful this.
You can see the detail of this in the report of "Redeploying Web Apps without Changing URL of Web Apps for new IDE".
In this workaround, I think that you can retrieve the edited cells by checking the difference between before and after the edited sheet. But, this is a simple script for explaining this workaround. So, if you use this workaround, please modify the script for your actual situation.
References:
Web Apps
Taking advantage of Web Apps with Google Apps Script
IMPORTXML
Referenced my posts.
Automatic Recalculation of Custom Function on Spreadsheet Part 1
Letting Users Running Google Apps Script on Google Spreadsheet without both Authorizing Scopes and Showing Script

Creating an onEdit trigger on a sheet owned by a service account

I have a GSheet that is owned by a service account, and I would like to add an “on edit” trigger to receive real-time edit notifications.
So far I've tried/looked at:
Simply adding the onEdit function to the bound script, however Google doesn’t allow Apps Script to be run on the script (issue is discussed more here):
Google Apps Script: The script cannot be run because it is owned by a service account. Please copy or transfer the project to a valid account before running.
Drive API push notifications - but that doesn’t give the per cell detail required, and regardless neither does it give a response faster enough; can be up to 3 mins according this SO post.
Changing the ownership of the GSheet - however other requirements require the GSheet to remain belonging to the service account.
Using the Script API - but it isn’t allowed to create triggers, and anyway service accounts can’t use the Script API.
Any other ideas, or is the fact Google simply doesn’t allow Apps Script to run under the service account a show stopper?”
I believe your goal is as follows.
You have a Google Spreadsheet created by the service account.
You want to use the OnEdit trigger on this Spreadsheet.
Updated on January 25, 2023:
I confirmed that in the current stage, your goal can be directly achieved using the installable OnEdit trigger. The sample flow is as follows.
Flow
1. Create a new Google Spreadsheet by service account.
Please create a new Google Spreadsheet by the service account. And, please copy the Spreadsheet ID. In this case, the owner of the Spreadsheet is the service account.
And, please share this Spreadsheet with your Google account as a writer. This is an important point.
2. Create a standalone script by your account.
Please create a new standalone script with your account. In this case, the owner of the standalone script is your account.
And, please copy and paste the following script to the script editor. And, please set the Spreadsheet ID of Spreadsheet of the service account to spreadsheetId and save the script.
function installTrigger() {
const spreadsheetId = "###"; // Please set the spreadsheet ID of your Spreadsheet.
const ss = SpreadsheetApp.openById(spreadsheetId);
ScriptApp.newTrigger("installedOnEdit").forSpreadsheet(ss).onEdit().create();
}
// In this pattern, please set your script in this function.
function installedOnEdit(e) {
e.range.setValue(JSON.stringify(e));
}
3. Install the OnEdit trigger.
In order to install the OnEdit trigger, please run installTrigger(). By this, the OnEdit trigger is installed to the function installedOnEdit. By this, when a cell of the above Spreadsheet is edited, installedOnEdit is run.
4. Testing.
Please edit the cell in the Spreadsheet of the service account. By this, the script of your standalone script is run, and you can see the event object in the edited cell.
References:
Installable Triggers
Old post:
Issue and workaround:
In the current stage, unfortunately, when the owner of Google Spreadsheet is the service account. The Google Apps Script cannot be run. By this, even when onEdit function is put to the script editor, this script cannot be run. It seems that this is the current specification of the Google side. Unfortunately, in the current stage, your goal cannot be directly achieved. I think that this is the current answer to your question.
But, I thought that a workaround might be able to be proposed. Fortunately, the built-in functions of the Spreadsheet can be also used in the Spreadsheet created by the service account. I thought that this might be able to be used as a workaround.
When I saw your question, I remembered the following my posts.
Automatic Recalculation of Custom Function on Spreadsheet Part 1
Letting Users Running Google Apps Script on Google Spreadsheet without both Authorizing Scopes and Showing Script
I thought that when these posts are used, a workaround might be able to be proposed. In this answer, I would like to propose a workaround for achieving your goal. So, please think of this as the pseudo OnEdit trigger.
The flow of this workaround is as follows.
Prepare Web Apps.
This Web Apps is deployed at the Google Apps Script project created by an account that the script can be run. Please be careful about this.
When the pseudo OnEdit trigger is used in "Sheet1", put IMPORTXML to another sheet (For example, it's "Sheet2".). IMPORTXML requests to the Web Apps.
This Spreadsheet is the Spreadsheet created by the service account.
When the cells in "Sheet1" are edited, the formula in "Sheet2" is run.
By this flow, when the cells in "Sheet1" are edited, the Google Apps Script of Web Apps can be run. This is the pseudo OnEdit trigger as this workaround.
Usage:
1. Create Google Apps Script for Web Apps.
In order to use Web Apps, please create Google Apps Script project by the account that the script can be run. In this case, as a sample situation, please create Google Apps Script project by your account instead of the service account.
2. Sample script:
Please copy and paste the following script to the script editor of the created Google Apps Script project.
function doGet(e) {
// do something
// Please set the script you want to use.
return ContentService.createTextOutput(`<result>Edited at ${new Date().toISOString()}</result>`).setMimeType(ContentService.MimeType.XML);
}
3. Deploy Web Apps.
The detailed information can be seen at the official document.
On the script editor, at the top right of the script editor, please click "click Deploy" -> "New deployment".
Please click "Select type" -> "Web App".
Please input the information about the Web App in the fields under "Deployment configuration".
Please select "Me" for "Execute as".
This is the importance of this workaround.
Please select "Anyone" for "Who has access".
In your situation, I thought that this setting might be suitable.
Please click "Deploy" button.
Copy the URL of the Web App. It's like https://script.google.com/macros/s/###/exec.
When you modified the Google Apps Script, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful this.
You can see the detail of this in the report of "Redeploying Web Apps without Changing URL of Web Apps for new IDE".
3. Testing.
Please prepare 2 sheets in Google Spreadsheet created by the service account. Those are "Sheet1" and "Sheet2".
Please put the following formula to "A1" of "Sheet2". In this case, please use your Web Apps URL. And, please confirm that the value is returned from the deployed Web Apps. If this cannot be done, please check the above flow again.
=IMPORTXML("https://script.google.com/macros/s/###/exec?values="&TEXTJOIN(",",TRUE,Sheet1!A:Z),"/result")
This formula is a sample formula. So, please modify the range and formula for your actual situation.
Please edit the cell on "Sheet1". When you use the above formula, please edit the cells in "A:Z". By this, the cell "A1" of "Sheet2" is refreshed, and by this refresh, Google Apps Script of Web Apps is run. This is the pseudo OnEdit trigger as this workaround.
Note:
When you modified the Google Apps Script, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful this.
You can see the detail of this in the report of "Redeploying Web Apps without Changing URL of Web Apps for new IDE".
In this workaround, I think that you can retrieve the edited cells by checking the difference between before and after the edited sheet. But, this is a simple script for explaining this workaround. So, if you use this workaround, please modify the script for your actual situation.
References:
Web Apps
Taking advantage of Web Apps with Google Apps Script
IMPORTXML
Referenced my posts.
Automatic Recalculation of Custom Function on Spreadsheet Part 1
Letting Users Running Google Apps Script on Google Spreadsheet without both Authorizing Scopes and Showing Script

Google sheets: is it possible to protect a range of cell such that it can only be modify with a script?

Hi I have a shared google sheet that enters date and time when a button is push. However, once enter I do not want the user to be able to modify the date. I know I can protect a range of cells but in this case I still need the macro/script to be able to enter the time.
Is this possible?
thanks.
I believe your goal as follows.
There are you (owner of Spreadsheet) and users.
Users run the script by clicking a button and the script puts the date value to a cell. At this time, you want to have already protected the cells for putting the date value from the users.
But, when the users click the button, you want to put the date values to the protected cells.
In your situation, Google Apps Script can be used.
From your replying, I could confirm my understanding is correct.
Issue and workaround:
When the script is run by clicking a button on Google Spreadsheet, the script is run as the user who clicked the button. So the authorization for scopes is required to be done as the users. I thought that this might be the answer of your replying I'm confused because would the script have the same permission as the person clicking the button?.
Under this situation, when the script puts a value to the cell protected by the owner, an error like You are trying to edit a protected cell or object. Please contact the spreadsheet owner to remove protection if you need to edit. occurs. So in order to avoid this error, it is considered that when the script is run as the owner, the issue will be avoided.
In this answer, I would like to propose a workaround. This workaround is as follows.
When the user runs the script by clicking a button on Spreadsheet, it runs the script as the owner by using Web Apps. When this workaround is used, please do the following flow.
Usage:
1. Prepare Spreadsheet.
Please create new Spreadsheet and create a button and assign the function of runWithWorkaround to the button. And, please protect the cell "A1" as the user of only owner. In this sample, the target cell is "A1".
2. Prepare script.
Please copy and paste the following script to the script editor of Google Spreadsheet. And, please set the sheet name which has the button.
// This function puts a date to cell "A1".
function putValue() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("A1").setValue(new Date());
}
// This function is for Web Apps.
function doGet() {
putValue();
return ContentService.createTextOutput();
}
// This function is used for testing "without using this workaround.".
function runWithoutWorkaround() {
putValue();
}
// This function is used for testing "with using this workaround.".
function runWithWorkaround() {
const url = "https://script.google.com/macros/s/###/exec"; // <--- Please replace this URL with your Web Apps URL.
UrlFetchApp.fetch(url, {headers: {authorization: `Bearer ${ScriptApp.getOAuthToken()}`}});
// DriveApp.getFiles() // This comment line is used for automatically detecting the scope "https://www.googleapis.com/auth/drive.readonly" for using Web Apps.
}
3. Deploy Web Apps.
The detail information can be seen at the official document.
On the script editor, at the top right of the script editor, please click "click Deploy" -> "New deployment".
Please click "Select type" -> "Web App".
Please input the information about the Web App in the fields under "Deployment configuration".
Please select "Me" for "Execute as".
This is the important of this workaround.
Please select "Anyone with Google account " for "Who has access".
In this case, the user is required to use the access token for requesting to Web Apps.
Please click "Deploy" button.
When "The Web App requires you to authorize access to your data" is shown, please click "Authorize access".
Automatically open a dialog box of "Authorization required".
Select own account.
Click "Advanced" at "This app isn't verified".
Click "Go to ### project name ###(unsafe)"
Click "Allow" button.
Copy the URL of Web App. It's like https://script.google.com/macros/s/###/exec.
When you modified the Google Apps Script, please redeploy as new version. By this, the modified script is reflected to Web Apps. Please be careful this.
Copy and paste the retrieved Web Apps URL to the above script.
Because the script of Web Apps is modified, please redeploy the Web Apps as new version. By this, the latest script is reflected to the Web Apps.
4. Testing.
When above workaround is used, the following result is obtained.
Note:
When you modified the Google Apps Script, please redeploy as new version. By this, the modified script is reflected to Web Apps. Please be careful this.
When several users use the button simultaneously, to use the lock service might be suitable. When the lock service is used, the function runWithWorkaround is as follows.
function runWithWorkaround() {
var lock = LockService.getDocumentLock();
if (lock.tryLock(10000)) {
try {
const url = "https://script.google.com/macros/s/###/exec";
UrlFetchApp.fetch(url, {headers: {authorization: `Bearer ${ScriptApp.getOAuthToken()}`}});
} catch(e) {
throw new Error(e);
} finally {
lock.releaseLock();
}
}
// DriveApp.getFiles() // This comment line is used for automatically detecting the scope for using Web Apps.
}
Above sample script is the simple sample script for explaining the methodology of this workaround. Please be careful this. So if you use this workaround, please modify your actual script using this workaround.
References:
Web Apps
Taking advantage of Web Apps with Google Apps Script

How to make a button in a Google Apps Script web app that executes a function in that project?

I would like publish a web app coded in Google Apps Script in a Google Page that has a button that runs a certain function within that Apps Script project. It is assumed that only my Google Account would use that button and my goal is to create and process spreadsheets when the button is pressed (creating a new spreadsheet using data from another one).
How to do this? I managed to deploy and publish a web app and I've also managed it's interaction with a given spreadsheet (displaying data from a sheet so far). My issue is that I do not know where to start with coding a button that does things.
I believe your goal as follows.
You want to create new Spreadsheet when a button on Web Apps, which was created by Google Apps Script, is clicked.
From My issue is that I do not know where to start with coding a button that does things., you want to know the method for achieving above process.
In this case, I think that google.script.run can be used. When google.script.run is used, the communication between HTML side and Google Apps Script side can be run.
As a simple script for achieving above goal, I would like to introduce as follows.
Sample script:
HTML&Javascript side: index.html
Please copy and paste the following script to the script editor as index.html.
<button onclick="sample()">Create Spreadsheet</button>
<script>
function sample() {
google.script.run.createSpreadsheet();
}
</script>
In this case, when a button is clicked, sample() is run.
Google Apps Script side: Code.gs
Please copy and paste the following script to the script editor as Code.gs.
function doGet() {
return HtmlService.createHtmlOutputFromFile("index");
}
function createSpreadsheet() {
// do something for "creating a new spreadsheet using data from another one"
SpreadsheetApp.create("sample name");
}
From I managed to deploy and publish a web app and I've also managed it's interaction with a given spreadsheet (displaying data from a sheet so far)., I think that you have already been able to deploy the Web Apps.
In this sample script, when you open the Web Apps, you can see a button. When you click the button, google.script.run.createSpreadsheet() is run. By this, createSpreadsheet() at Google Apps Script side is run. As the result, new Spreadsheet is created. (In this case, the created Spreadsheet is put to the root folder.)
Note:
When you modified the script of Web Apps, please redeploy the Web Apps as new version. By this, the latest script is reflected to the Web Apps. Please be careful this.
I think that the various sample scripts using google.script.run can be seen at Stackoverflow. These are useful for understanding how to use it.
References:
Class google.script.run
Web Apps

Using private sheets with tabletop.js

I've used tabletop.js [1] in the past and is amazing! You can simply do anything you want seriously.
The only problem I saw is that you need to publish your spreadsheets to the web, which of course is really risky if you are working with sensitive data.
I'm in need now of using it in a project with sensitive data, so I was hoping someone can guide me on how to use it with spreadsheets that are not published to the web.
I've been searching for this for a long time without any success but seems that tabletop.js does support private sheets (here's the pull request that added this option [2]).
In fact, looking at the documentation they included it [1]:
authkey
authkey is the authorization key for private sheet support.
ASK: How am I suppose to use the authkey? can someone provide me with an example so I can try?
Thanks in advance!
[1] https://github.com/jsoma/tabletop
[2] https://github.com/jsoma/tabletop/pull/64
How about this answer?
Issue and workaround:
At "tabletop.js", from the endpoint (https://spreadsheets.google.com/feeds/list/###/###/private/values?alt=json) of request, it seems that "tabletop.js" uses Sheets API v3. And when authkey is used, oauth_token=authkey is added to the query parameter. In this case, unfortunately, it seems that the private Spreadsheet cannot be accessed with it. From this situation, unfortunately, I thought that in the current stage, "tabletop.js" might not be able to use the private Spreadsheet. But I'm not sure whether this might be resolved in the future update. Of course, it seems that the web-published Spreadsheet can be accessed using this library.
So, in this answer, I would like to propose the workaround for retrieving the values from Spreadsheet as the JSON object.
Pattern 1:
In this pattern, Google Apps Script is used. With Google Apps Script, the private Spreadsheet can be easily accessed.
Sample script:
When you use this script, please copy and paste it to the script editor and run the function myFunction.
function myFunction() {
const spreadsheetId = "###"; // Please set the Spreadsheet ID.
const sheetName = "Sheet1"; // Please set the sheet name.
const sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
const values = sheet.getDataRange().getValues();
const header = values.shift();
const object = values.map(r => r.reduce((o, c, j) => Object.assign(o, {[header[j]]: c}), {}));
console.log(object) // Here, you can see the JSON object from Spreadsheet.
}
I thought that this might be the simple way.
Pattern 2:
In this pattern, the Web Apps created by Google Apps Script is used. When the Web Apps is used, the private Spreadsheet can be easily accessed. Because the Web Apps is created with Google Apps Script. In this case, you can access to the Web Apps from outside by logging in to Google account. And, the JSON object can be retrieved in HTML and Javascript.
Usage:
Please do the following flow.
1. Create new project of Google Apps Script.
Sample script of Web Apps is a Google Apps Script. So please create a project of Google Apps Script. In order to use Document service, in this case, Web Apps is used as the wrapper.
If you want to directly create it, please access to https://script.new/. In this case, if you are not logged in Google, the log in screen is opened. So please log in to Google. By this, the script editor of Google Apps Script is opened.
2. Prepare script.
Please copy and paste the following script (Google Apps Script) to the script editor. This script is for the Web Apps.
Google Apps Script side: Code.gs
function doGet() {
return HtmlService.createHtmlOutputFromFile("index");
}
function getObjectFromSpreadsheet(spreadsheetId, sheetName) {
const sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
const values = sheet.getDataRange().getValues();
const header = values.shift();
const object = values.map(r => r.reduce((o, c, j) => Object.assign(o, {[header[j]]: c}), {}));
return object;
}
HTML&Javascript side: index.html
<script>
const spreadsheetId = "###"; // Please set the Spreadsheet ID.
const sheetName = "Sheet1"; // Please set the sheet name.
google.script.run.withSuccessHandler(sample).getObjectFromSpreadsheet(spreadsheetId, sheetName);
function sample(object) {
console.log(object);
}
</script>
spreadsheetId and sheetName are given from Javascript side to Google Apps Script side. From this situation, in this case, getObjectFromSpreadsheet might be instead of "tabletop.js".
3. Deploy Web Apps.
On the script editor, Open a dialog box by "Publish" -> "Deploy as web app".
Select "Me" for "Execute the app as:".
By this, the script is run as the owner.
Select "Only myself" for "Who has access to the app:".
In this case, in order to access to the Web Apps, it is required to login to Google account. From your situation, I thought that this might be useful.
Click "Deploy" button as new "Project version".
Automatically open a dialog box of "Authorization required".
Click "Review Permissions".
Select own account.
Click "Advanced" at "This app isn't verified".
Click "Go to ### project name ###(unsafe)"
Click "Allow" button.
Click "OK".
Copy the URL of Web Apps. It's like https://script.google.com/macros/s/###/exec.
When you modified the Google Apps Script, please redeploy as new version. By this, the modified script is reflected to Web Apps. Please be careful this.
4. Run the function using Web Apps.
You can test above scripts as follows.
Login to Google account.
Access to the URL of Web Apps like https://script.google.com/macros/s/###/exec using your browser.
By this, you can see the retrieved JSON object at the console.
Note:
When you modified the script of Web Apps, please redeploy the Web Apps as new version. By this, the latest script is reflected to the Web Apps. Please be careful this.
References:
Web Apps
Taking advantage of Web Apps with Google Apps Script