gas spreadsheet authorization - google-apps-script

I'm new to goole apps script.
I have created a simple test script and I would like to open a spreadsheet and read some information.
My script is authorised, I published it with:
Execute the app as: me
Who has access to the app: Anyone, even anonymous
I can open the spreadsheet manually from drive so I have access to it.
But when I run the script it shows me: "Authorisation is required to perform that action."
Here is the script:
function doGet(e) {
var doc = SpreadsheetApp.openById("0AvQ4qcr20AT2dHBDRm5QYzFqTkdyNk9jU3hRUDFoakE");
return ContentService.createTextOutput( doc.getName());
}
Do I need to make some additional authorisation using oauth or something like this ?
If so I would appreciate if I get any example.

You have to run the doGet function - or any other function in the same project - just once from the script editor before you can use the webapp.
This will trigger the authorization process (showing one or more popups depending on the services you use in that project) and is a necessary step.

Related

Google Apps Script PropertiesService Permissions for AddOns

I'm currently developing an Editor Add-on for Google Sheets using Apps Script. There's an onOpen function that sets up the menu items.
I also have a CONFIG variable in the root (not in any function) like this:
const CONFIG = {}
function setProperties_(){
CONFIG.tmpSheetId = PropertiesService.getScriptProperties().getProperty('TMP_SHEET_ID');
}
setProperties_();
If I run any functions from within the script editor, everything runs fine. No issues. However, when I do a test deployment I get the error below from the moment the onOpen() runs:
You do not have permission to call getScriptProperties
I've tried adding various script scopes from here but nothing works.
When you first install the add-on (the entry for the add-on becomes visible in the sheet menu), it runs in AuthMode.NONE that allows you to inspect only the current user's locale. Please refer to the table here Google Add-On Authorization Lifecycle
As Cooper pointed out, calling setProperties_() in global scope occurs before your add-on is authorized by the user. Move the function call to a nested function and make sure you are through with the authorization flow first.

Add to Google Sheets with a Google Apps Script Web App

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.

How to expose a web API to a google apps script

I have a google apps spreadsheet, and I want to write a script that can be accessed via any browser, will generate a response using data from the spreadsheet, and return it to the browser.
I put in a lot of data in the spreadsheet that is required for my app. I want to automate the process of getting the data from the spreadsheet and integrating into the app before creating a build. Right now I run the script manually, download the result, and copy it into the app's data folder.
Ideas?
You can interact with your script via HTTP Post
doPost Trigger
Here is a little example of a doPost function
function doPost(e){
return ContentService.createTextOutput(SpreadsheetApp.openById(id).getSheetByName(name).getDataRange().getValues())
}

receive parameters from another server POST request using apps script

I have some technical question about using apps script.
A third party server is sending me parameters in POST method and the request looks like this: https://script.google.com/macros/s/AKfycbyJYVLO46T1LnQKktaMrROclCOqgawcVfZaRbm_oXfJaMIYcPj8/exec?value=$postback_params_test$ (so I need to receive $postback_params_test$ as value)
I used doPost(e) function but with no success, I thing the problem is because apps script is based on client java script and it c'ant talk with server language, am I right? or there is an option to do it anyway through apps script?
my code:
function doPost(e){
var param = e.parameter.value;
var doc = SpreadsheetApp.openById("106jpepwZZWXtpO4Id45qmJovV68q_DIqpEmTQ0khf4E");
var cell = doc.getRange('a1');
cell.setValue(param);
}
8.6
Image added:
enter image description here
When deployed as a web app with settings "execute as: me" and "who has access to the app: anyone, even anonymous" your example code works.
Did you authorize the code? Before you can run it as a web app, you must run doPost() (or another function) once manually from within the script editor, so you can grant the appropriate permissions to the script.
If it's not the authorization issue, you can add a MailApp.sendemail() call to help you troubleshoot.
function doPost(e) {
MailApp.sendEmail('YOUR-EMAIL HERE','TEST doPost()',JSON.stringify(e));
This way you'll receive an email showing the raw request coming from the other server.
Be sure to re-run the script manually after adding the MailApp line so you can authorize it to send email, and update the published version.

Creating time-based trigger from WebApp

I am trying to create a time-based trigger from a Google Apps Script published as a WebApp.
Update: this WebApp shall be run as the user accessing it, not as the Apps Script owner.
Whenver I even try to access the ScriptApp services, I get an error in the frontend:
Authorization is required to perform that action.
Sample code to reproduce this would be:
function doGet(request) {
ScriptApp.newTrigger('test');
return ContentService.createTextOutput("OK");
};
function test() {
// noop
}
Is this something that is not possible? Is it possible to somehow automatically add a trigger that runs a method based on time with the user giving the authorization?
Under what credentials is your web app run ? As the user running the app or as yourself.
From the error, it looks like it is run under the author's credentials.
In this case, you should authorize the application explicitly before you can distribute the URL. To do so, open the script editor and choose any function and try to run the function using the little 'play' button in the script editor. You'll be asked to provide permissions after which you can use the Ui App