0

I want to create several individual URLs (in other words, endpoints) for a particular sheet in a given document. I have data (not much, about 200-250 rows). Basically, data structure is exactly the same every year (ID, Country, Title, Name etc.). I have managed to transform selected cells into proper JSON format and deploy the following as a Web Application.

I'm currently using the doGet() method, which body can be found below.

I tried to use official Google Documentation, but it seems to me that I do not fully understand it.

https://developers.google.com/apps-script/guides/web#url_parameters

function doGet(e) 
{
    var sheetName = "Lorem Ipsum";
    var sheetID = "SDJHFLKSDJFLJSD94UF940FUJ9FS90FJ049JW9F";
    var book = SpreadsheetApp.openById(sheetID);
    var sheet = book.getSheetByName(sheetName);
    var JSON = convertSheet2JsonText(sheet);
    return ContentService.createTextOutput(JSON.stringify(JSON)).setMimeType(ContentService.MimeType.JSON);
}

[
    {
        "ID": 1,
        "PUBLISHED": true,
        "NAME": "Lorem Ipsum",
        "TITLE": "Lorem Ipsum",
        "SOURCE": "Lorem Ipsum",
        "DATE": "14 June 2019",
        "COUNTRY": "Poland"
    },
    {
        "ID": 2,
        "PUBLISHED": false,
        "NAME": "Lorem Ipsum",
        "TITLE": "Lorem Ipsum",
        "SOURCE": "Lorem Ipsum",
        "DATE": "05 August 2019",
        "COUNTRY": "USA"
    }
]

Basically, just after deploy, I get a link, but it contains data only from one sheet to which I provided the ID. The expected result should look like this:

  • URL: ../macros/s/../exec/data-2018
  • URL: ../macros/s/../exec/data-2019
  • URL: ../macros/s/../exec/data-2020

1 Answers1

0

Problem

Unfortunately, WebApps deployed on Google infrastructure don't support routing per se, you get only one URL for each project ending with /exec for production and /dev for development environment respectively.

Solution

Fortunately, you can append a query string to this URL if you want to specify which function to run / ID to use, etc. Think of the doGet() / doPost() as the router in this case: /exec?param1=value1&param2=value2. Every parameter you pass to query string will be conveniently available from the event object under either parameter or parameters key.

Sample

If I understood you correctly, you want to route the WebApp by sheetName property, right? Then, your URL should look like /exec?name=data-2018 (btw, don't forget to properly encode it, especially if your names contain whitespaces) and you should access this parameter in doGet:

function doGet(e) {
    var param     = e.parameter; //access query params;
    var sheetName = param.name;

    var sheetID = 'yourId';
    var book = SpreadsheetApp.openById(sheetID);
    var sheet = book.getSheetByName(sheetName);
    var JSON = convertSheet2JsonText(sheet);
    return ContentService.createTextOutput(JSON.stringify(JSON)).setMimeType(ContentService.MimeType.JSON);
}

Useful links

  1. Event object structure (+one of my older answers);
  2. URL encoding reference;