Instead of returning an HtmlService object, you can pass data using the jQuery $.getJSON method and retrieve data from the doGet function using ContentService . Google Apps Script does not accept CORS, so using JSONP is the best way to get data from your script as well. See this post for more details .
CodePen working example
I share your HTML and scripts for clarity. None of the HTML has changed from your original example.
Code.gs
function doGet(e) { var returnValue;
JS Client
$(function() { $(".google-upload").click(function() { var appUrl = "https://script.google.com/macros/s/AKfycbyUvgKdhubzlpYmO3Marv7iFOZwJNJZaZrFTXCksxtl2kqW7vg/exec"; var query = appUrl + "?url="; var popupUrl = query + $('.google-upload').attr("data-url") + "&callback=?"; console.log(popupUrl)
You can check the error message by passing an empty string in the data-url parameter. The message is returned to the console as well as to the page for the user.
Change 3.7.18
The above solution has problems with authorization flow control. After researching and discussing with the drive engineer ( see the section here ), I reworked this in a self-serving example based on the Script API Application and launching the project as an API executable, not a Script application for web applications. This will allow you to access the [run](https://developers.google.com/apps-script/api/reference/rest/v1/scripts/run) method [run](https://developers.google.com/apps-script/api/reference/rest/v1/scripts/run) outside the Apps Script web application.
Customization
Follow the instructions in the Google Apps Script for JavaScript . The Apps Script project must be self-contained (not document-related) and published as an executable API. You will need to open the Cloud Console and create the OAuth credentials and API key.
The instructions use the Python server on your computer. I use Node JS server, http-server , but you can also put it online and check from there. You will need to whiten your source to the cloud console.
Customer
Since this is self-service, you will need a simple HTML page that authorizes the user through the OAuth2 API via JavaScript. This is preferable because it supports user login, allowing you to use multiple API calls for your Script without re-authorization. The code below works for this application and uses the authorization flow from the Google Quick Launch guides.
index.html
<body> <button id="authorize-button" style="display: none;">Authorize</button> <button id="signout-button" style="display: none;">Sign Out</button> <button onclick="uploadDoc()" style="margin: 10px;" id="google-upload" data-url="https://calibre-ebook.com/downloads/demos/demo.docx">Upload doc</button> <pre id="content"></pre> </body>
index.js
Script Applications
Script application code does not need to be significantly changed. Instead of returning using the ContentService we can return simple JSON objects that will be used by the client.
function uploadDoc(e) { Logger.log(e); var returnValue = {};
It was difficult for me to get the code name CodePen, so I have an example that is safely hosted on my own site using the code above. Feel free to inspect the source and see the live Apps Script project.
Please note that the user must re-authorize when adding or changing areas in the Apps Script project.