Apps Script Refused to Connect - google-apps-script

I am trying to learn Apps Script and some fron-end web dev. I wrote some some code in Apps Script and I am trying to render it in a Google Site.
Here is the doGet function that I use in my Apps Script:
function doGet() {
var template = HtmlService.createTemplateFromFile('Index');
return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
Interestingly, the script renders itself when I use the Google-given URL:
https://sites.google.com/corp/view/vrajlinkshortener
However, that is not the case when I enter the custom domain:
www.wharton.ml
I checked the documentation and I still could not figure out why a custom domain would prohibit Apps Script form working.
Any tips? Thanks!

You need to set the option XFrameOptionsMode to ALLOWALL.
XFrameOptionsMode
https://developers.google.com/apps-script/reference/html/x-frame-options-mode
Setting XFrameOptionsMode.ALLOWALL will let any site iframe the page, so the developer should implement their own protection against clickjacking.
return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
As stated in the comments below (Shan Eapen Koshy said) check that your browser is logged into one Google account only.

Just add below, it is working
function doGet(e){
var temp = HtmlService.createTemplateFromFile('html')
return temp.evaluate().setTitle("Booking Window").setSandboxMode(HtmlService.SandboxMode.IFRAME).setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
}

Related

How to convert my google sheet script to web app?

I have created a Google Sheet to process a Sales and send mail from it. But I have found a limitation, it works only on PC and cannot be used on an android phone using the Sheets app. Is there a way to convert my code to a web app, use Sheets as my database and use the interface from my phone?
Create a deployment of the project as a web-app. you'll probably want to use the Test Deployment for developing and you will need the doGet(e) function to get the page up and running.
function doGet(e)
{
var html = HtmlService.createHtmlOutputFromFile("your_HTML_file_here");
return html;
}
The function above creates the webpage but the rest of the code will end up in the html file that contains the web page.
To interact with google and its services you can use
google.script.run
This allows the user to run google script code from an html page.
Not sure what you mean by "convert". You can publish your project as web app (for that you need doGet() function and some kind of UI) and then access it on mobile via URL.

Google Picker shows up blank when used in Google Sites

To all Google Apps experts - please help me solve this. I'm rather stuck and I've not found any explanation yet on why this problem exists at all. I've included a live example to demonstrate the problem.
The problem may be view at this location ...
https://sites.google.com/a/growthhq.net/faulty-picker/
A Google Apps script made available in a Google Sites using the Apps Script gadget has stopped functioning recently. The crazy thing is that the app works perfectly using the 'dev' and 'exec' urls for the app directly, but when embedding the app in Sites (with the 'exec' url and Apps Script gadget), and the button is clicked, the form comes up blank.
I know that the UiApp is deprecated (sadly, this had some good attributes) but I don't want to change the rest of the code at this stage.
What can I do to get the Picker to show correctly in sites? Is the referrer inadequately specified? I'm at a total loss.
The activated referrers are:
*.google.com
*.googleusercontent.com
I have extracted (and simplified) the code portions for testing purposes. This working test code follows:
// refer to https://developers.google.com/apps-script/guides/dialogs#file-open_dialogs for setting up OAuth
function doGet()
{
var app = UiApp.createApplication();
var buttonHandler = app.createServerHandler('utilityPicker');
var button = app.createButton('Open Picker', buttonHandler)
app.add(button);
return app;
// This is a dummy to activate
DriveApp.getRootFolder();
}
function utilityPicker(e){
var app = UiApp.getActiveApplication();
var authToken = ScriptApp.getOAuthToken();
Logger.log(authToken);
var docPicker = app
.createDocsListDialog()
.setOAuthToken(authToken)
.setDialogTitle('Select a Google Spreadsheet or Form to be used by this Workspace')
.setMultiSelectEnabled(true)
.addView(UiApp.FileType.FOLDERS)
.showDocsPicker()
;
Logger.log(docPicker);
docPicker.addView(UiApp.FileType.SPREADSHEETS);
var handler = app.createServerHandler('pickerPrimarySpreadsheet');
docPicker.addSelectionHandler(handler);
return app;
}
function pickerPrimarySpreadsheet(e)
{
Logger.log(e.parameter);
}
Please help.
got the same problem,
found some google group forum where some people had to
add
.setOrigin('https://script.google.com')
to the pickerbuilder
still does not solve my problem, if u got a working solution to use on google sites please share

edit google document remotely

I'm trying to add a feature to my website as follows:
Clicking a button appends text to a Google document.
Obviously I will need to create an Apps Script in the drive. The question is how to trigger the Apps Script from my website. You can assume that I am the owner of the drive/document and so have permissions to edit it in any way I like.
I have looked at these topics:
Workarounds : How to edit native google documents programatcally?item
How to programmatically manipulate native google doc files
It seems they are all actions performed from the drive itself and not triggered remotely.
I have also looked up the Apps Script API in Google but have not found a way to do this. Is it even possible?
Yes, it is possible.
First write an Apps script that changes your desired document. Then deploy it as a web-app running as you that anyone has access, even anonymous. Check out the guides at Apps Script page to see how to write your script as a web-app.
Your script will then have a public url, which you can call from your website and have it run "remotely" normally.
To provide an example of what Henrique suggests, here is a small webapp that adds text to a publicly viewable document I own (it doesn't need to be public except for anyone here to check it works !)
I wrote it using UiApp but you could of course use HTMLService if you prefer...
The app runs as me but is accessible to anyone even anonymous.
// publicly viewable test doc url : https://docs.google.com/document/d/1THzBTURxGr2CdUmcZ7i2zD-RM8I3im2JCSHI3BHlkeM/edit
function doGet(){
var app = UiApp.createApplication().setTitle('docEdit');
var panel = app.createAbsolutePanel().setSize('100%','100%').setStyleAttributes({'padding':'40px','backgroundColor':'lightBlue'});
var text = app.createTextArea().setName('text').setPixelSize(500,300);
var grid = app.createFlexTable().setId('grid');
grid.setText(0,0,'Add your text').setWidget(1,0,text);
var handler = app.createServerHandler('writeText').addCallbackElement(panel);
grid.setWidget(2,0,app.createButton('update document',handler).setId('btn'));
app.add(panel.add(grid));
return app;
}
function writeText(e){
var doc = DocumentApp.openById('1THzBTURxGr2CdUmcZ7i2zD-RM8I3im2JCSHI3BHlkeM');
var now = Utilities.formatDate(new Date(),Session.getScriptTimeZone(),'MMM/dd/yyyy # hh:mm:ss');
var body = doc.getBody();
body.appendParagraph('Append text on '+now+' : '+e.parameter.text);
doc.saveAndClose();
var app = UiApp.getActiveApplication();
var grid = app.getElementById('grid');
grid.setWidget(3,0,app.createHTML('Thanks,<br>Your text has been added to the document'));
app.getElementById('btn').setEnabled(false).setHTML('Button disabled');
return app;
}

UrlFetchApp fetch a google apps script

From a Google apps script I'm trying to use UrlFetchApp to fetch another Google apps script. I tried to make it as basic as possible but it's still giving me an error.
The first script called 'frontend' executes as the user and the second script 'backend' executes as the owner of 'backend'.
frontend
var backend = 'https://script.google.com/macros/s/AKfycbwdwQdYYMwqofjEqFn3ozz_LqQ87qj7ZX19sYmelX9dUtP8aNxf/exec';
function doGet(e) {
return UrlFetchApp.fetch(backend)
}
backend
function doGet(e) {
return doPost(e);
}
function doPost(e) {
var app = UiApp.createApplication();
app.add(app.createLabel('Backend'));
return app;
}
frontend is published here
extra information
Logger.log(UrlFetchApp.getRequest(backend)) =>
{useIntranet=false,
followRedirects=true,
payload=,
method=get,
validateHttpsCertificates=true,
contentType=application/x-www-form-urlencoded,
url=https://script.google.com/macros/s/AKfycbwdwQdYYMwqofjEqFn3ozz_LqQ87qj7ZX19sYmelX9dUtP8aNxf/exec}
Using instead an html form I can correctly sent POST/GET parameters from one google script to another using hidden input elements to define parameters.
If I try to do what the html form is doing using javascript - jquery ajax the request returns an error.
I'm very confused as to why a simple html form within a google script can request another google script but a javascript request from a google script to another google script is denied by what appears to be security policy issues.
You dont say how youve published them exactly but i bet your backend isnt published to allow anonymous users. Do so.

Google Gadget API remote-content issue

In my Google Gadget JavaScript, I have an URL to call from Google Apps Script.
function getFileId(postData) {
var params = {};
var url = "https://script.google.com/a/macros/mycompany.com/s/AAABBBCCCDDEEE-FFF-GGG/exec";
params[gadgets.io.RequestParameters.POST_DATA] = postData;
params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
gadgets.io.makeRequest(url , responseFileId, params);
};
So, when the function responseFileId() has returned obj.text, it actually goes to the mycompany.com login screen. That means it needs authentication from that Gadget I did. Didn't that Google Gadget hold my current domain's Google credential for running the Google Apps Script at "https://script.google.com/a/macros/mycompany.com/s/AAABBBCCCDDEEE-FFF-GGG/exec"?
I tried to run manually with Google Chrome browser, (https://script.google.com/a/macros/mycompany.com/s/AAABBBCCCDDEEE-FFF-GGG/exec) and it returns value without showing up the login screen...
Can someone tells what step has been missing for this? Has this something to do with Google OAuth?
Instead of doing this. I found a better idea => Using tags for remote website.