0
votes

Again newbee in app scripting I am trying to do the following

How to store array values in a variable for event object while on form submit i.e I need to get all values from form responses column in a sheet J to AF from a single row into a variable

something like var value = j,k,l,m,n,o(obviously as string without any spl chars like ,.etc)

instinctively there should be better way get all the values in a variable? tried some loop since the range starts from in-between columns(J-AF) of the sheet cant get it right

function onFormSubmit(e) {
Logger.log("%s", JSON.stringify(e));
//Get information from form and set as variables
var amount = [e.values[9],e.values[10],e.values[11],e.values[12],e.values[13],e.values[14],...e.values[31]];
Logger.log('Lets see if its here:'+ amount);
}
2

2 Answers

0
votes

Try this:

Don't forget to change the sheet name if needed.

And create the trigger.

function onFormSubmit(e) {
 var ss=SpreadsheetApp.getActive();
 var sh=ss.getSheetByName('Sheet1');
 sh.getRange(sh.getLastRow()+1,10,1,e.values.length).setValues([e.values]); 
}
0
votes

The e object from onFormSubmit(e) has the following structure:

{ 
authMode: {... },
  namedValues: 
   { 'Question 2': [ 'Answer 2' ],
     Timestamp: [ '3/2/2020 9:48:53' ],
     Question: [ 'Answer' ] },
  range: { columnEnd: 3, columnStart: 1, rowEnd: 6, rowStart: 6 },
  source: {},
  triggerUid: '3xxxxxx825600xxx',
  values: [ '3/2/2020 9:48:53', 'Answer', 'Answer 2' ] 
}

As you can see, there are a couple of properties that can be useful for your case. (namedVales and values).

It is up to you to choose which to use.

An example:

function onFormSubmit(e) {

  // Get values from response and put them in named variables
  var amountDict = {
    'Question 2' : e.namedValues['Question 2'],
    'Question' : e.namedValues['Question'],
    'Timestamp' : e.namedValues.Timestamp, 
    moreCols:'...', colAB: e.namedValues[10]
  };

  // Log them 
  console.log(amountDict);

  // Get values from response and put them in unnamed variables
  var amountList = [e.values[0],e.values[2], '...', e.values[3]];
  // Log them 
  console.log(amountList);

}

Output:

enter image description here