Вопрос

I'm experimenting with using a Form to create a new announcement on our Google Sites page. The form asks for the title and body of the post. The form is also linked to a spreadsheet. In the form a can be input and will display in the spreadsheet. The console log in the script also shows the paragraph break.

The Sites API seems to strip out the and replace it with a space. For example, what was supposed to be 2 paragraphs, each with one sentence, becomes two sentences in the announcement. How do I get the paragraph break to display in the new announcement created in this script? Any help would be much appreciated. Thank you in advance.

var posted = "POSTED";

for (var i = 0; i < data.length -1; ++i) { 

var timeStamp = data[i][0];  
var name = data[i][1];       
var title = data[i][2];
var body = data[i][3];
var status = data[i][4];

Logger.log(title);
Logger.log(body);
Logger.log(status);

if (status != posted) { 

  var url = 'myintranetsite.com'
  var page = SitesApp.getPageByUrl(url)

  // Got this code from another post, script won't fail in event of duplicate titles
  var announcements = page.getAnnouncements({ 
                                         includeDrafts: false,
                                         includeDeleted: false,
                                         search: title });

  if (announcements.length > 0) {
  title += ' (' + announcements.length + ')'; 

  var newPost = page.createAnnouncement(title, body);
Это было полезно?

Решение 2

I assume the question about "paragraph" markers.

Body should be HTML https://developers.google.com/apps-script/reference/sites/page#createAnnouncement(String,String)

I suspect you want to do a find and replace.

Or just put HTML into the form. e.g. add:

 "<p>" 

Другие советы

Yes, turns out it was a question about paragraph markers and replacing special characters. Thank you!

Here's the code that worked to replace the new line char from the form response with
and insert the para markers.

var newBody = body.replace(/\n/g, '<br />');   // replaces new line with <br /> 

var htmlBody = "<p>";
htmlBody += newBody;
htmlBody += "<p />";

var newPost = page.createAnnouncement(title, htmlBody);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top