Samuele
Samuele

Reputation: 79

Publish a Google Spreadsheet through Google Apps Scripts #2

I am trying to publish a web app through the script code. I read this post but I can't make it work, maybe it's a slightly different situation. I have got a script that copies a spreadsheet: in the script, I want to publish the new copy of the spreadsheet as a web app and, if it's possibile, retrieve its public url (anyone should be able to access to it).

Here it is my code:

  // Make a copy of the spreadsheet
  var repliesFile = responseTemplate.makeCopy('risposte', newFolder);
  var repliesId = SpreadsheetApp.openById(repliesFile.getId());

  // pubblish the new copy as a web app
  Drive.Revisions.update({published: true, publishedOutsideDomain: true, publishAuto: true}, repliesId, 1);

  // of course, this doesn't work.. I need a way to get the web app public url of the new copy
  var webAppUrl = ScriptApp.getService().getUrl(); 

I got an 404 error. What am I doing wrong? Drive Api v.2 is enabled in the script. Thanks for any help you can give me!

Upvotes: 1

Views: 569

Answers (1)

Tanaike
Tanaike

Reputation: 201378

  • You want to publish the copied Spreadsheet.
  • You want to retrieve the URL of the published Spreadsheet.
  • You want to achieve this by modifying your script.

If my understanding is correct, how about this answer?

Modification points:

  • In your script, I think that repliesId is not correct. It's the Spreadsheet object. Please use repliesFile.getId() as the file ID.
  • Unfortunately, in the current stage, the URL like https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml cannot be retrieved. But you can https://docs.google.com/spreadsheet/pub?key=### spreadsheetId ### as the URL of published Spreadsheet.

Modified script:

When your script is modified, please modify as follows.

From:

Drive.Revisions.update({published: true, publishedOutsideDomain: true, publishAuto: true}, repliesId, 1);

To:

var fileId = repliesFile.getId();
Drive.Revisions.update({published: true, publishedOutsideDomain: true, publishAuto: true}, fileId, 1);
var url = "https://docs.google.com/spreadsheet/pub?key=" + fileId;
  • url can be used as the URL of the published Spreadsheet.

Note:

  • In this case, please enable Drive API at Advanced Google services.

References:

Upvotes: 3

Related Questions