Crouzilles
Crouzilles

Reputation: 813

Adding new line to text in Google spreadsheet cell via apps script

I have an apps script which takes email addresses from one spreadsheet from multiple cells and adds them them to another spreadsheet into 1 cell only. Currently the email addresses are added to that cell and separated by a ", ".

I would like to add the email addresses into that same cell but add a new line after each address.

I know it is possible to have new lines in a cell when manually adding text, by typing CTRL-Enter.

How can this be achieved in apps script?

I have so far tried to append "\n" or "\r" or "\r\n" to the string, to no avail, so I have reverted my code back to adding ", " instead.

sheet = SpreadsheetApp.getActiveSheet();
sheet.clear();

sheet.appendRow(["Num Emails", "Emails"]);

var LMEmails = "";
var count  = 0;
for (var i = 0; i < reviewers.LMEmails.length; i++) {
  if (count) {
    LMEmails += ", " + reviewers.LMEmails[i];
  } else {
    LMEmails += reviewers.LMEmails[i];
  }
  count++;
}

data = [count, LMEmails];

sheet.appendRow(data);

If anyone could help, I would very much appreciate it

Regards Crouz

Upvotes: 10

Views: 22926

Answers (3)

Stu Marlow
Stu Marlow

Reputation: 581

Google Spreadsheets seems to ignore the new line if there are no characters after it.

Using "/n" works fine as long as you add at least one other character after it, this could be a space as shown below.

For example:

var myString = 'First Bit' + "\n " + "Second Bit";

Upvotes: 2

Ben
Ben

Reputation: 151

After searching for the same question, this was my solution:

var stringToDisplay = 'FirstBit' + String.fromCharCode(10) + 'SecondBit';

then

workSheet.getRange(1,1).setValue(stringToDisplay);

output will be in a single cell, :
FirstBit
SecondBit

Upvotes: 15

Alan Wells
Alan Wells

Reputation: 31300

It should be:

data = [[count, LMEmails]];

Upvotes: 0

Related Questions