0

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!

Community
  • 1
  • 1
  • 1
    Sorry but if you already have the value, why would you ask it again? – Jacobvdb Feb 19 '14 at 00:16
  • Hi Jacobvdb, i have the value in the speadsheet, i need it to be displayed in the form, this is why i need help understanding how the above code works. – user3321028 Feb 19 '14 at 06:10
  • It is not possible to set a default value for a [textItem](https://developers.google.com/apps-script/reference/forms/text-item#) in a Google Form with the formApp. Also see this [post](http://stackoverflow.com/questions/20320670/can-i-fill-in-textitem-by-google-apps-script) – Jacobvdb Feb 19 '14 at 13:35
  • If so, how come the above worked for previous case? I need the same think, just i don't understand the code so i could modify it for my case, can someone help me? – user3321028 Feb 19 '14 at 19:59
  • The script above is the [answer 2](http://stackoverflow.com/questions/20320670/can-i-fill-in-textitem-by-google-apps-script/21284556#21284556) on the other [post](http://stackoverflow.com/questions/20320670/can-i-fill-in-textitem-by-google-apps-script) – Jacobvdb Feb 19 '14 at 20:11

0 Answers0