Report: Publishing Various Google Docs with Same URL using Google Apps Script

Gists

This is a sample method for publishing various Google Docs files with the same URL using Google Apps Script.

By updating on May 25, 2022, the content got to be able to be embedded as a full page in the new Google site. Ref In this method, this is used.

Usage

1. Create a Google Docs.

First, as a simple sample, please create a new Google Spreadsheet. And please copy the URL like https://docs.google.com/spreadsheets/d/{spreadsheetId}/edit.

2. Create a Google Apps Script project.

You can use both the standalone script type and the container-bound script type.

3. Sample script

Please copy and paste the following script to the script editor of the created Google Apps Script project. And, please set the Google Spreadsheet URL to googleDocs.

function doGet() {
  const googleDocs =
    "https://docs.google.com/spreadsheets/d/{spreadsheetId}/edit";

  const html = `<iframe src="${googleDocs}" style="border: none; width: 95vw; height: 95vh; padding: 0; margin: 0;"></iframe>`;
  return HtmlService.createHtmlOutput(html);
}

4. Deploy Web Apps.

The detailed information can be seen at the official document. In this case, it supposes that you are using new IDE. Please be careful this.

  1. On the script editor, at the top right of the script editor, please click “click Deploy” -> “New deployment”.
  2. Please click “Select type” -> “Web App”.
  3. Please input the information about the Web App in the fields under “Deployment configuration”.
  4. Please select “Me” for “Execute as”.
  5. Please select “Anyone” for “Who has access”.
    • For testing this script, I thought that this setting might be suitable.
  6. Please click “Deploy” button.
  7. Copy the URL of the Web App. It’s like https://script.google.com/macros/s/###/exec.

5. Create a new Google site

Pleases create a new Google site. And, please set the top page as the full page for embeding the content. Ref and Ref I thought that the animation gif might be useful for understanding how to set it.

And, please set your Web Apps URL as the full page content to the created Google site. And, please publish the Google site.

6. Testing

When the above setting is done and you access your published Google site, you can see the situation of the top image in this post on your browser.

  • For the owner of Goog Apps Script, Google site, and Google Spreadsheet.

    • In this sample, you can see and edit the Spreadsheet.
    • Script editor cannot be opened.
    • You can run the script using the custom functions.
    • When a button assigned to the Google Apps Script function is set, when you click the button, you can run the script.
    • You can use the script using the simple triggers of OnEdit, OnOpen.
    • You can use the script using the installed triggers which are OnEdit, OnChange, and so on.
  • For the users who are NOT the owner of them.

    • When the Spreadsheet is not publicly shared, when users access your Google site, the users cannot see the Spreadsheet. Because the users have no permissions.
    • When the Spreadsheet is publicly shared as the writer when users access your Google site, the users can see and edit the Spreadsheet.
    • In this sample, you can see and edit the Spreadsheet.
    • Script editor cannot be opened.
    • Users can run the script using the custom functions.
    • Even when the users click the button for running the script, the script is not run.
    • Triggers which are OnEdit, OnChange, and so on cannot be used.
    • Users can NOT use the script using the simple triggers of OnEdit, OnOpen.
    • Users can use the script using the installed triggers which are OnEdit, OnChange, and so on.

Note

  • In this method, you can freely change from Google Spreadsheet to Google Document, Google Slides, Google Forms, and so on. And, the access URL is not changed. Users can always access your set Google Docs and other page using the same URL.

  • About the above sample, when you want change the Google Spreadsheet to Google Document, please modify googleDocs.

    • When you modified the Google Apps Script, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful this.
  • Also, I think that this method can be used for publishing your original site created by Web Apps with Google Apps Script and HTML.

Reference

 Share!