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);
}
CodePudding user response:
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:
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:
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.