How to include data in a URL for a Google Apps Scr

2019-09-16 18:24发布

问题:

I've made my first little test web app and have that "technology" working, as per:

The next step is to enable a customer to be able to click on a status URL in an email so they can view the scheduling status of their job. So, the question:

Is it possible to create a URL which the webapp can then parse and pull data for?

A URL something like: https://script.google.com/a/macros/example.com/s/AKfycbwMgBLtVP8Ur8GKNpGxxjna_zr5BlegmvlFqXQW1g6q5UfksAg/exec/+123+sample+street+samplesuburb

and then the web app would pull and display the scheduling info related to that address.

Many thanks ~

回答1:

You can inspect the 'e' object of the doGet() function to get URL parameters

 // yourUrl/?a=1&b=2&c=3&c=4

function doGet(e){

 e.queryString // will be a=1&b=2&c=3&c=4
 e.parameter; // will be {"a": "1", "b": "2", "c": "3"}. For parameters that have multiple values, this only returns the first value
 e.parameters; // will be {"a": ["1"], "b": ["2"], "c": ["3", "4"]}. Returns array of values for each key.



}

See 'URL parameters' section here https://developers.google.com/apps-script/guides/web



回答2:

As a working test, I used:

A web app URL with a parameter, like: https://script.google.com/a/macros/example.com/s/AKfycbwMgBLhu88Ur8GKNpGxxjna_zr5BlegmvlFqXQW1g6q5UPksAg/exec?street=65_Fields_Ave ...with "street=65_Fields_Ave" concatenated to the web app URL from spreadsheet data for a specific customer, to be included in their Booking email.

And read the street like (the real script will test for suburb too):

function doGet(e) {
  var paramStreet = e.parameter.street;          // format like "123_Sample_Street"
  var street = paramStreet.replace(/#|_/g,' ');  // format like "123 Sample Street"

Then searched for the row in the spreadsheet and presented the data via:

var html = HtmlService.createTemplateFromFile('index');
html.name = fullName;
html.street = street;
html.status = schedStatus;

return html.evaluate();

And in the HTML file:

<div> 
  <h1>Scheduling Status</h1>
  <p>Primary Contact: <?= name; ?></p>
  <p>Street: <?= street; ?></p>
  <p>Status: <?= status; ?>
</div>

Note that the web app needs to be published as a new version after each change:

Works like a charm! Thank you Anton Dementiev.