1
votes

I am attempting to build a web app that will create a Google Doc from a template and populate it with user provided data. Using Google's quickstart example in the documentation, I can successfully authorize and access the Google Drive file system. Now I need to programmatically open a template Google Doc (or even create one from scratch) and add the data.

This is rather easily done using App Script's Document Service (the DocumentAppclass). So I can do something like:

function createDoc(contentArray) {
  var doc = DocumentApp.create('Sample Document');
  var body = doc.getBody();
  var rowsData = contentArray; // data submitted with HTML form passed as arg
  body.insertParagraph(0, doc.getName())
      .setHeading(DocumentApp.ParagraphHeading.HEADING1);
  table = body.appendTable(rowsData);
  table.getRow(0).editAsText().setBold(true);
}

in a standalone App Script and successfully create the new Google Doc on Google Drive. I can't figure out how to execute this App Script from my external web app. Is there a way to do this or do I need to find a different way to create Google Docs (and add content) using just the Drive API?

EDIT:

here is the GET request from my web app:

var gurl = "https://script.google.com/macros/s/AKfycbwMHKzfZr1X06zP2iEB4E8Vh-U1vGahaLjXZA1tk49tBNf0xk4/exec";
$.get(
    gurl,
    { name: "john", time: "2pm",},
    function(data) {
      console.log(data);
    },
    "jsonp"
)

and here is my doGet():

function doGet(e) {

  var result = "";
  var name = e.parameter.name;

  Logger.log(name);

  try {
    result = "Hello " + name;
  } catch (f) {
    result = "Error: " + f.toString();
  }

  result = JSON.stringify({
    "result": result
  });  

  var doc = DocumentApp.create('ballz3');
  var body = doc.getBody();
  var rowsData = [['Plants', 'Animals'], ['Ficus', 'Goat'], ['Basil', 'Cat'], ['Moss', 'Frog']];
  body.insertParagraph(0, doc.getName())
      .setHeading(DocumentApp.ParagraphHeading.HEADING1);
  table = body.appendTable(rowsData);
  table.getRow(0).editAsText().setBold(true);
  Logger.log('DOc Name: ' + doc.getName());

  return ContentService
  .createTextOutput(e.parameter.callback + "(" + result + ")")
  .setMimeType(ContentService.MimeType.JAVASCRIPT);   

}
1
Take a look at this guide - to be able to execute your webapp, you'll need a doGet() or a doPost() function and a versioned deployment of your WebApp - Oleg Valter
@oleg valter this is exactly what i need. I can successfully handle a request (with doGet() or doPost() and send back a response, but it doesn't seem to be executing any of my Document.app calls. Possibly a permissions thing? - Daveh0
You have to deploy the script webapp with the following parameters: execute as me and accessible to anyone even anonymous. That's because when your app is calling the doget function via the URL it is actually anonymous. - Serge insas
If you think it would create a security issue you can imagine a couple of security parameters to make it really difficult to hack. The documentation shows how to add parameters to an URL and how to handle it in a doget function. I've used that configuration a lot of time to(for example) restrict the access to certain people in my domain by giving each one of them an unique url. - Serge insas
@Daveh0, sorry saw your question only now - I believe Serge answered your question in full, it is indeed a permissions issue (enable anonymous access). Or you can take an API executable route as Alberto suggested (both have their pros & cons, ofc) - Oleg Valter

1 Answers

0
votes

In order to run a script from an external location like your web app you need to follow some set-up and use the Script API. The documentation provided below has a great example on how to run your scripts from outside.

Additionally, you can use the APIs directly, with services like OAuth to use the APIs directly in a way that can save you some time and make your code more simple. Using OAuth can provide you with simple API requests. To use it:

  • Go to the link provided below, select the desired scope (Drive for this example).
  • Exchange the authorization token for refresh/access tokens.
  • Proceed to configure the request. Here you can set all the parameters for the request, and even select from the existing operations the scope has available (“List possible operations” button).
  • The resulting request will look like the one below:

    GET /drive/v3/files HTTP/1.1 Host: www.googleapis.com Content-length: 0 Authorization: Bearer [YOUR-TOKEN]

  • Beneath it you will see the server response to the request.

Documentation URL: https://developers.google.com/apps-script/api/how-tos/execute

OAuth Playground: https://developers.google.com/oauthplayground/