Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 377
Hanny94 Avatar asked Nov 21 '13 23:11

Hanny94


1 Answers

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.

like image 97
Chris Cirefice Avatar answered Oct 09 '22 04:10

Chris Cirefice