Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 768
Crouzilles Avatar asked Mar 19 '15 16:03

Crouzilles


People also ask

How do I add a new line of text in Google Sheets?

Manually add a new line in the same cell (Keyboard Shortcut)Double-click on the cell in which you want to add a line break (or select it and then press F2). Place the cursor where you want to insert the line break. Hold the ALT key and then press the Enter key (or Control + Option + Enter if you're using a Mac)

How do you do a line break in Google Sheets app?

Just tap enter to start a new line and type your text in the second line. This way you can insert as many new lines as you want in your Google Sheets Android mobile app. It's very simple, right?

How do I make multiple lines of text in Google spreadsheet?

Thankfully, you can – to type information into more than one line in a Google Sheets cell, click on the cell in question and type the first line of your content in. Then, press Alt + Enter on your keyboard (or Option + Enter if you use a Mac) to get to a new line.

How do I insert a line break in Google Sheets concatenate?

To insert a new line within a cell in Google Sheets, follow these steps: Type the text that you want to be on the first line within the cell. While the cell is still being edited, press Ctrl + Enter on the keyboard, and the cursor will go to a new line / a new line will be added within the same cell.


2 Answers

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

like image 127
Ben Avatar answered Oct 29 '22 18:10

Ben


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";
like image 27
Stu Marlow Avatar answered Oct 29 '22 17:10

Stu Marlow