I want to create a web app script. I need to add the url generated by the deployment in a cell of another spreadsheet so that it allows me to obtain its ID.
The script only works on the spreadsheet from which I did the deployment. If I execute the URL from the cell of another spreadsheet, it returns the wrong ID (the ID of original spreadsheet).
Is it possible for the app to automatically detect the spreadsheet ID from which cell it was opened? or should it only be done by sending parameters?
I have the following code:
function doGet(e) {
var id = getidSheet();
return ContentService.createTextOutput(id);
}
function getidSheet(){
var idSheet = SpreadsheetApp.getActiveSpreadsheet().getId();
return idSheet;
}
CodePudding user response:
A web app can't determine the place where the URL is located. If you need to send the ID of the spreadsheet holding the link, you need to add it to the URL. This could be done by adding the spreadsheet id as a value of a query string parameter or as the URI fragment (put it with #
as prefix)
Let say that the following is the original URL
http://www.example.com
Using a URL parameter (pair of paramenter name and parameter value)
https://www.example.com?spreadsheetId=put_here_the_spreadsheet_id
Using a URI fragment
https://www.example.com#put_here_the_spreadsheet_id
Then on your web app's doGet function use the event object properties
function doGet(e){
// Log the spredsheetId parameter
console.log(e.parameter.spreadsheetId);
// Log the URL fragment (anything after #)
console.log(e.queryString.split('#'))[1]));
retunr HtmlService.createHtmlOutput('<p>Hello world!</p>');
}
Reference