2

I'm building a solution in which the amount of parameters added to an HTTP get request is variable. This means the URL can carry 1 to n amount of parameters. The parameters in the URL are identified with a standard name and attached index. Here is a sample URL:

https://script.google.com/macros/s/xxxxxxx/exec?fileName=2020-12-15&fridgeItems=2&fridgeItem0=ValueOfFridgeItem0&fridgeItem1=ValueOfFridgeItem1

How do I interpret the variable amount of Parameters in my doGet() Function in .gs?

EDIT: I want to create with this request the header of a new google sheet. The variable amount of parameters is supposed to be the column headers. E.g. if the request contains 5 parameters, I want to add in the first row of the google sheet into columns 1 to 5 the respective values of the parameters.

I tried the following code but it is not working:

function doGet(request) {
var FileNameString = request.parameter.fileName; //name of the new .gs file
var FridgeItems = request.parameter.fridgeItems; //amount of parameters/column headers passed to .gs

var sheet = SpreadsheetApp.create(FileNameString);

   for(var i=0; i<FridgeItems ; i++){
   sheet.getRange(1,i+2)
   .setValues(
     sheet.appendRow([request.parameter.fridgeItem&i]);
     );
   }
 }
}
Hamed
  • 5,867
  • 4
  • 32
  • 56
  • Can you provide a screenshot of the desired output? I'm not sure I understand this. You want each key to be a header, and the corresponding value to be appended below the header? Are there keys with multiple values? Are the headers set up already, or you want to check whether the header already exists and just append the value below if that's the case? There are several problems with the code you shared, but I won't suggest modifications until I'm sure I understand your purpose. – Iamblichus Dec 15 '20 at 12:20
  • @Iamblichus thanks for swift response. i updated the question to be more precise as well as modified the source code based on current status of research. what I want to achieve is that the value of each key should represent the header. the headers are not yet set up but need to be set up dynamically. The appendRow function was only used due to the missing appendColumn function. As the the sheet is blank, appendRow will add to row 1. – Andreas Kurz Dec 15 '20 at 13:09

1 Answers1

1

Goal:

When a GET request is made, you want to create a new spreadsheet whose name will be the value of the request parameter key fileName, and all the other values from all the other request parameters will be appended to the only sheet of the created spreadsheet.

Issues:

  • You don't know how to loop through the different request parameters.
  • You are providing a Sheet (that's what's returned by the method appendRow(rowContents)) as an argument of setValues(values). This is not a valid argument; you should provide a 2D array instead.

Solution:

  • Retrieve the value from fileName and remove this property from the object request.parameter, using the delete operator.
  • Since you want the values of the rest of parameters, you can use Object.values() to retrieve an array with these values.
  • Use Spreadsheet.appendRow() in order to append these values to the created spreadsheet.

Code snippet:

function doGet(request) {
  const parameters = request.parameter;
  const fileName = parameters.fileName;
  delete parameters.fileName;
  const headers = Object.values(parameters);
  const spreadsheet = SpreadsheetApp.create(fileName);
  spreadsheet.appendRow(headers);
  return ContentService.createTextOutput("Headers appended!");
}

Edit:

If you want to make sure the headers are set in the same order as in the query, you can use request.queryString instead, which returns the query string portion of the URL, so in this case would be like:

fileName=2020-12-15&fridgeItems=2&fridgeItem0=ValueOfFridgeItem0&fridgeItem1=ValueOfFridgeItem1

You could then use a combination of map and filter in order to retrieve the values (excluding the one corresponding to fileName):

function doGet(request) {
  const headers = request.queryString.split("&")
                                        .map(param => param.split("="))
                                        .filter(param => param[0] !== "fileName")
                                        .map(param => param[1]);
  const fileName = request.parameter.fileName;
  const sheet = SpreadsheetApp.create(fileName)
  sheet.appendRow(headers);
  return ContentService.createTextOutput("Headers appended!");
}
Iamblichus
  • 18,540
  • 2
  • 11
  • 27
  • Thanks a lot. This works in general! However, it seems that the order of request parameters is volatile? The 'header' is created but not in the same order as the parameters have been provided. Multiple runs deliver different result in order. Is there the chance to influence the order? Thanks for support! – Andreas Kurz Dec 15 '20 at 17:24
  • @AndreasKurz I added an alternative code to my answer so that the order is kept. – Iamblichus Dec 16 '20 at 11:19