Dear Programmers,
I am turning to you for a small help. This can be considered a repost, but I think this question, if it will be answered will help a lot of people who are not familiar with JS.
So, the scenario is like this:
I want to calculate and track my travelled distances using Google Docs, for this I have created a small form which contains a few fields: Date, Miles at Start, Miles at End, Route description.
Every time, Miles at End, logically are = to the Miles at Start for the next trip.
This is why I need a script that will populate the field "Miles at Start" with the last value of "Miles at End".
To simplify the work, I have dedicated a cell in the spreadsheet which contains always the last value.
This issue is treated here: Is it possible to 'prefill' a google form using data from a google spreadsheet?
But I don't really understand how the code works:
/**
* Use Form API to generate pre-filled form URLs
*/
function betterBuildUrls() {
var ss = SpreadsheetApp.getActive(); // I need to put here the spreadsheet name? and in what format?
var sheet = ss.getSheetByName("Sheet1"); // I should put here the sheet name? in betwwen ""?
var data = ss.getDataRange().getValues(); // I don't have a range, I have a cell that contains the value, How i put it here?
var formUrl = ss.getFormUrl(); // Use form attached to sheet - Should I add something here?
From this I part, I am totaly out :)
var form = FormApp.openByUrl(formUrl);
var items = form.getItems();
// Skip headers, then build URLs for each row in Sheet1.
for (var i = 1; i < data.length; i++ ) {
// Create a form response object, and prefill it
var formResponse = form.createResponse();
// Prefill Name
var formItem = items[0].asTextItem();
var response = formItem.createResponse(data[i][1]);
formResponse.withItemResponse(response);
// Prefill Birthday
formItem = items[1].asDateItem();
response = formItem.createResponse(data[i][2]);
formResponse.withItemResponse(response);
// Get prefilled form URL
var url = formResponse.toPrefilledUrl();
Logger.log(url); // You could do something more useful here.
}
};
Please help me!
Thank you in advance!