How Can Line Breaks in Google Spreadsheets be Preserved When Posting to Google Sites?

Hanny94 picture Hanny94 · Nov 22, 2013 · Viewed 8.7k times · Source

I have a script on Google app script. This script find a data simply.

my code :

var content=this.spreadsheet.getSheetByName("sheet1").getRange("C1:C26").getValues();
this.summary = contents[4][0];

I find my data , no prob but , my data has line breaks and my webpage on Google sites shows the result without line breaks.

It's a prob of convert with GetValue () ?


my data on a Cell of Spreadsheet :

blabab---
bla
abla---

bla

the result on a Google Site

blabab---bla abla---bla

Answer

Chris Cirefice picture Chris Cirefice · Nov 22, 2013

The solution is the following:

  1. Get your string (in the cell) that you want to post to your Google Site.
  2. Replace all line breaks (\n) in the string with the HTML version (<br />)

Something like the following:

function lineBreakTest() {
  var cellWithLineBreaks = SpreadsheetApp.getActiveSheet().getRange("a1").getValue();
  Logger.log(cellWithLineBreaks);

  cellWithLineBreaks = cellWithLineBreaks.replace(/\n/g, '<br>');

  Logger.log(cellWithLineBreaks);

  // Post to your Google Site here. Logger.log is just used to demonstrate.

}

The reason you have to do this is because Spreadsheets uses normal text line breaks \n in its cells. However, Google sites uses HTML format, so you need to do the 'conversion'. This answer is also a helpful source.