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

1

8

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

Throe answered 21/11, 2013 at 23:40 Comment(2)
Can you clarify what you're asking? You say "my data doesn't have break_line and my webpage on Google sites show the result without break_line". If that is what is happening, what is the problem? Also, by break_line do you mean a line break as in \n?Shred
Yes ,in my cell of spreadsheet i have many line Break and on my WebPage on Google sites , i don't have one line break or /n or <br /> yesThroe
S
4

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.

Shred answered 22/11, 2013 at 0:37 Comment(8)
Thanks Chris but , in my Google sites i see : ( for example : <br /><br /><br /><br />The main objective of this project is to ... <br /> ) This code is display on the page.html in the same Google script projet ( i use a textArea on the html page, so the prob concerns the html page in google script and not the cell on the spreadsheet ? )Throe
You will need to use HTML Box, which is a "Gadget" in Google Sites, or use methods in the Sites class that use HTML as a parameter. See the Sites classShred
I can't use a gadget in my Google sites, i have a template HTMl test_data.html and my String is in an object TP : My string : TP.summary; my code : <?= TP.summary ?> <? if(this.edit) {?> </textarea><?}?> i have remplace with you code Chris but , i see <br /> in string , not a break_line ...Throe
Well to my knowledge Google Sites by default render HTML in posts, both Announcements and on Pages. You could try replacing with <br> instead of <br />, and that may do the trick. If your text is not being rendered as HTML, then you need to use \n as a delimiter for line breaks. \r is a carriage-return, which functions pretty much the same. You can try using that as well. However, if your text is being rendered as HTML, you need to use an HTML tag that gives you a line break. <br> and <p> are similar in my experience. Try one of those alternatives :)Shred
As an additional note... since you're using the HTML <textarea> tag... you should see this post. Pretty much a direct answer to your question ;) at least... I think. Also see documentation on the textarea tag.Shred
Thx Chris but , the tag br don't working, i see the tag on my Google sites page , but le tag p i don't see the tag but no break line on my google sites page ... :(Throe
the code withe replace fonction: testtest</p><p>test</p><p></p><p>testtest The Google site page : </p><p>testtest</p><p>test</p><p></p><p>testtest it's the same with <br>Throe
Well that means that your Google side is not rendering HTML, or you are not setting the HTML correctly. It's likely the latter. Look at the documentation to use the correct methods for creating Announcements or Pages. Both take HTML as a parameter, so if you use those methods properly, your HTML (<br>, <p> tags, etc.) will render as it should.Shred

© 2022 - 2024 — McMap. All rights reserved.