0
votes

I am fetching contents from Spreadsheet and adding it as items in form through Google Apps script. When I have a text in multiple lines (texts with new line breaks) in a cell, upon generating the form, it appears in a single line. I need to have it as exactly as I am giving in the sheet. Is there any way to achieve this using Apps script?

Here is the code I used to copy data from Sheet to crate item in form:

     var ss = SpreadsheetApp.openByUrl(mySheetURL);
     var sheet = ss.getSheets()[0];
     var cellRange = 'C2:C6'
     var ques = sheet.getRange(cellRange).getValues();
     var newForm = FormApp.openByUrl(myformURL);
     var QOD = newForm.addMultipleChoiceItem();
     QOD.setTitle(ques);

Content in my Spreadsheet Cell:

Content in form:

enter image description here

1
Please add a brief description of you search/research efforts as is suggested in How to Ask.Rubén
Please share the code that your using to add the text to the form. If you use a paragraph long answer and put in separate lines with a control enter, when the form loads them into the spreadsheet it preserves the line feeds.Cooper
I am using multipleChoice Item. And I am fetching data from Sheet to create a form items, not loading form data into the spreadsheet. @CooperKeekz

1 Answers

0
votes

Apps Script retrieves and passes the line break as intended from Sheets to Form

The only problem is how you retrieve the values:

  • cellRange[1][1] is not a valid request, because a range is a class, not a 2-D array.

  • Opposed to it values indeed returns you a grid and you can retrieve the grid elements by their indices. Mind that the indices start with [0], not with [1].

  • Consequently getRange(cellRange).getValues()[1][1] would return you null because cellRange = 'C2:C6' contains only one column and it has the index [0].

Correct request:

     var ss = SpreadsheetApp.openByUrl(mySheetURL);
     var sheet = ss.getSheets()[0];
     var cellRange = 'C2:C6'
     var ques = sheet.getRange(cellRange).getValues()[0][0];
//also possible: var ques = sheet.getRange('C2').getValue();
     var newForm = FormApp.openByUrl(myformURL);
     var QOD = newForm.addMultipleChoiceItem();
     QOD.setTitle(ques);