Skip to content

Instantly share code, notes, and snippets.

@giastfader
Created January 28, 2019 11:14
Show Gist options
  • Select an option

  • Save giastfader/304b398ba92f34188019d27ac150f811 to your computer and use it in GitHub Desktop.

Select an option

Save giastfader/304b398ba92f34188019d27ac150f811 to your computer and use it in GitHub Desktop.
Using GForm to populate a GDoc Template
//this object maps GSheet column into convenient placeholders used into GDocs templates
var columns_placeholder_map = {
"chronological information" : "date_time",
"Please, insert the company name" : "company_name",
"Company director first name" : "first_name",
"Company director last name" : "last name",
"Price (in $)" : "price"
//...
//...
}
/*
Returns an object having keys as in columns_placeholder_map values, and values equals to the given row
*/
function transformRowInJSON(row_number) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cols = sheet.getLastColumn();
//we get the first row as form field (label) names
var labels = sheet.getRange(1, 1, 1, cols).getValues()[0]
//now we get the values of the given row
var dataRange = sheet.getRange(row_number, 1, 1, cols);
var data = dataRange.getValues();
var row_data = data[0];
//now we have labels and row_data, let's build the final JSON object to return
var result = {};//this is the returned object
for (var i=0;i<row_data.length;i++){
var label = labels[i];
var key = columns_placeholder_map[label.trim()];
//maybe some label is not mapped
if (key) {
var value = row_data[i];
result[key] = value;
}
}
/*
At this point we will have something like this:
{
"date_time":"2019-01-28 07:22:34", //actually this is a Date object
"company_name": "ACME ltd",
"first_name": "John",
"last name": "Doe"
"price":"1000"
}
*/
return result;
}//transformRowInJSON
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment