Google Apps Script in Google Doc - How to merge 2 format in 1 paragraph

Viewed 45

How do I combine both into 1 paragraph instead but with different formatting? Something like concatenate?

function insertControlPara() {
  var paraControlStyle1 = {};
  paraControlStyle1[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
  paraControlStyle1[DocumentApp.Attribute.FONT_SIZE] = 11;
  paraControlStyle1[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.JUSTIFY;
  paraControlStyle1[DocumentApp.Attribute.LINE_SPACING] = 1.5;
  paraControlStyle1[DocumentApp.Attribute.SPACING_BEFORE] = 10;

  var paraControlStyle2 = {};
  paraControlStyle2[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
  paraControlStyle2[DocumentApp.Attribute.FONT_SIZE] = 8;
  paraControlStyle2[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.JUSTIFY;
  paraControlStyle2[DocumentApp.Attribute.LINE_SPACING] = 1.5;
  paraControlStyle2[DocumentApp.Attribute.SPACING_BEFORE] = 10;
  paraControlStyle2[DocumentApp.Attribute.FOREGROUND_COLOR] = '#980000';

  var text1 = body.setText('Paragraph <Normal Text, Calibri, 11PT, 1.5 Spacing, Space before Paragraph> ');
  var text2 = body.setText('Paragraph <Normal Text, Calibri, 8PT, 1.5 Spacing, Space before Paragraph> ');
  var paraText = text1 + text2;
  body.appendParagraph(paraText);
}

The result should be this: enter image description here

2 Answers

Use the function merge()

Using the merge() function concatenates the second paragraph with the previous paragraph. Appending two existing paragraphs, like in your case, requires the usage of the function getChild(). In your case, you may use the following script:

function insertControlPara() {
  var paraControlStyle1 = {};
  paraControlStyle1[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
  paraControlStyle1[DocumentApp.Attribute.FONT_SIZE] = 11;
  paraControlStyle1[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.JUSTIFY;
  paraControlStyle1[DocumentApp.Attribute.LINE_SPACING] = 1.5;
  paraControlStyle1[DocumentApp.Attribute.SPACING_BEFORE] = 10;

  var paraControlStyle2 = {};
  paraControlStyle2[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
  paraControlStyle2[DocumentApp.Attribute.FONT_SIZE] = 8;
  paraControlStyle2[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.JUSTIFY;
  paraControlStyle2[DocumentApp.Attribute.LINE_SPACING] = 1.5;
  paraControlStyle2[DocumentApp.Attribute.SPACING_BEFORE] = 10;
  paraControlStyle2[DocumentApp.Attribute.FOREGROUND_COLOR] = '#980000';

  //Changes start here
  var body = DocumentApp.getActiveDocument().getBody();
  var par1 = body.getChild(0); //assigns the first paragraph to par1
  var par2 = body.getChild(1); //assigns the second paragraph to par2
  par1.setAttributes(paraControlStyle1);
  par2.setAttributes(paraControlStyle2);
  par2.merge(); //merges the two paragraphs

}

In this modified script, I used the following test case:

test data

Wherein Test 1 will be referred to as par1 and Test 2 will be referred to as par2 in the script. After storing the paragraphs in their respective variables using the getChild function, the setAttributes() function was applied based on your settings before merging the paragraphs using the merge() function. The output should look like this:

output

Please take note that there should be no new line between both paragraphs before merging since the new line will also be counted as a child.

sample

References:

Additional Information:

When using the function appendParagraph() on an empty document, the function adds another child. Hence, the empty entity will be counted as the first child (child(0)) while the appended paragraph will be counted as the second child (child(1)). Hence, the usage of the function removeChild() is needed. In this case, you may modify the script this way:

function insertControlPara() {
  var paraControlStyle1 = {};
  paraControlStyle1[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
  paraControlStyle1[DocumentApp.Attribute.FONT_SIZE] = 11;
  paraControlStyle1[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.JUSTIFY;
  paraControlStyle1[DocumentApp.Attribute.LINE_SPACING] = 1.5;
  paraControlStyle1[DocumentApp.Attribute.SPACING_BEFORE] = 10;
  paraControlStyle1[DocumentApp.Attribute.FOREGROUND_COLOR] = '#000000';

  var paraControlStyle2 = {};
  paraControlStyle2[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
  paraControlStyle2[DocumentApp.Attribute.FONT_SIZE] = 8;
  paraControlStyle2[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.JUSTIFY;
  paraControlStyle2[DocumentApp.Attribute.LINE_SPACING] = 1.5;
  paraControlStyle2[DocumentApp.Attribute.SPACING_BEFORE] = 10;
  paraControlStyle2[DocumentApp.Attribute.FOREGROUND_COLOR] = '#980000';
  
  //changes start here
  var body = DocumentApp.getActiveDocument().getBody();

  var text1 = body.appendParagraph('Test1').setAttributes(paraControlStyle1);
  var text2 = body.appendParagraph('Test2').setAttributes(paraControlStyle2);
  body.removeChild(body.getChild(0)); //remove empty child
  var par1 = body.getChild(0); //first paragraph
  var par2 = body.getChild(1); //second paragraph

  par2.merge();

}

Which will result to:

test

I think the answer to your question is to use the merge method.

Example

Related