First of all, I would like to thank you for your help in solving problems prior to this project, which was of great value in solving past problems. I'm working with google apps script to format the titles of a document within tables. The normal text is this:
After the script runs it looks like this:
The script is inserting the text inside tables in a normal way, as desired, however in addition to this formatting, I need to style the titles. Example: Title 1 usually has an Arial 18 font, without boldface and I want to change that to a Roboto 18 font with boldface. I tried to work with custom styles of google apps, but in the script processing the formatting is lost specifically when it passes through this line of code.
I have already tried to recover the tables and format them after the update process, but the system does not recognize the tables as tables after the update process, and only the last formatted title remains in the desired format, as shown in the second image. See some prints of my debugging process. The first title is changed and placed within the table and the formatting is applied and the inserted table is recognized: When the script reaches the saveAndClose () point of the second method, the customization of the previous title disappears: At the end of the process, only the last customized title remains in the desired format. I already tried to recover the inserted tables to perform the update in the style of the text of the second column, but the script does not recognize the tables. It recognizes only one, and in fact, in this document I have 4 tables.
Here is a script for verification:
function verifiStyle(){
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragrafs = body.getParagraphs();
for(var i = paragrafs.length - 1; i >= 0; i--){
var attr = paragrafs[i].getAttributes();
for(var at in attr){
if(at == "HEADING" & attr[at] == "HEADING1"){
VerifTitle1(i);
}
else if(at == "HEADING" & attr[at] == "HEADING2"){
VerifTitle2(i);
}
else if(at == "HEADING" & attr[at] == "HEADING3"){
VerifTitle3(i);
}
else if(at == "HEADING" & attr[at] == "HEADING4"){
VerifTitle4(i);
}
else if(at == "HEADING" & attr[at] == "NORMAL"){
VerifTextoNormal(i);
}
}
}
var tables = body.getTables();
}
function VerifTitle1(value){
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var texto = body.getChild(value);
var ttt = texto.getText();
var cells = [
['', '']
];
var styleCell1 = {};
styleCell1[DocumentApp.Attribute.FONT_SIZE] = 20;
styleCell1[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
styleCell1[DocumentApp.Attribute.FOREGROUND_COLOR]='#888888';
styleCell1[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
var styleCell = {};
styleCell[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
styleCell[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
styleCell[DocumentApp.Attribute.FONT_SIZE] = 18;
styleCell[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000';
styleCell[DocumentApp.Attribute.HEIGHT] = 0.5;
body.removeChild(body.getChild(value));
var table = body.insertTable(value, cells);
table.getRow(0).getCell(1).appendParagraph(ttt).setHeading(DocumentApp.ParagraphHeading.HEADING2);
table.getRow(0).getCell(1).setAttributes(styleCell);
table.getRow(0).getCell(0).setWidth(2);
table.getRow(0).getCell(0).setAttributes(styleCell1);
table.setBorderColor('#ffffff');
table.getRow(0).editAsText().setBold(true);
const index = body.getChildIndex(table);
const documentId = doc.getId();
doc.saveAndClose();
const tableStart = Docs.Documents.get(documentId).body.content[index + 1].startIndex;
const tempStyle = {width: {magnitude :0, unit: "PT"}, dashStyle: "SOLID", color: {color: {rgbColor: {blue: 0}}}};
const resource = {requests: [
{updateTableCellStyle: {
tableStartLocation: {index: tableStart},
tableCellStyle: {borderTop: tempStyle, borderBottom: tempStyle, borderLeft: tempStyle, borderRight: tempStyle},
fields: "borderTop,borderBottom,borderLeft,borderRight"
}},
{updateTableCellStyle: {
tableRange: {
tableCellLocation: {tableStartLocation: {index: tableStart}, rowIndex: 0, columnIndex: 0}, rowSpan: 1, columnSpan: 1},
tableCellStyle: {
borderRight: {dashStyle: "SOLID", width: {magnitude: 3, unit: "PT"}, color: {color: {rgbColor: {red: 0.9372549019607843, green: 0.3254901960784314, blue: 0.3137254901960784}}}}
},
fields: "borderRight"
}}
]};
Docs.Documents.batchUpdate(resource, documentId);
table = body.getChild(value).asTable();
}
function VerifTitle2(value){
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var texto = body.getChild(value);
var ttt = texto.getText();
var cells = [
['', '']
];
var styleCell1 = {};
styleCell1[DocumentApp.Attribute.FONT_SIZE] = 18;
styleCell1[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
styleCell1[DocumentApp.Attribute.FOREGROUND_COLOR]='#888888';
styleCell1[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
var styleCell = {};
styleCell[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
styleCell[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
styleCell[DocumentApp.Attribute.FONT_SIZE] = 15;
styleCell[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000';
styleCell[DocumentApp.Attribute.HEIGHT] = 0.5;
body.removeChild(body.getChild(value));
var table = body.insertTable(value, cells);
table.getRow(0).getCell(1).appendParagraph(ttt).setHeading(DocumentApp.ParagraphHeading.HEADING2);
table.getRow(0).getCell(1).setAttributes(styleCell);
table.getRow(0).getCell(0).setWidth(2);
table.getRow(0).getCell(0).setAttributes(styleCell1);
table.setBorderColor('#ffffff');
table.getRow(0).editAsText().setBold(true);
const index = body.getChildIndex(table);
const documentId = doc.getId();
doc.saveAndClose();
const tableStart = Docs.Documents.get(documentId).body.content[index + 1].startIndex;
const tempStyle = {width: {magnitude :0, unit: "PT"}, dashStyle: "SOLID", color: {color: {rgbColor: {blue: 0}}}};
const resource = {requests: [
{updateTableCellStyle: {
tableStartLocation: {index: tableStart},
tableCellStyle: {borderTop: tempStyle, borderBottom: tempStyle, borderLeft: tempStyle, borderRight: tempStyle},
fields: "borderTop,borderBottom,borderLeft,borderRight"
}},
{updateTableCellStyle: {
tableRange: {
tableCellLocation: {tableStartLocation: {index: tableStart}, rowIndex: 0, columnIndex: 0}, rowSpan: 1, columnSpan: 1},
tableCellStyle: {
borderRight: {dashStyle: "SOLID", width: {magnitude: 3, unit: "PT"}, color: {color: {rgbColor: {red: 0.9372549019607843, green: 0.3254901960784314, blue: 0.3137254901960784}}}}
},
fields: "borderRight"
}}
]};
Docs.Documents.batchUpdate(resource, documentId);
}
function VerifTitle3(value){
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var texto = body.getChild(value);
var ttt = texto.getText();
var cells = [
['', '']
];
var styleCell1 = {};
styleCell1[DocumentApp.Attribute.FONT_SIZE] = 16;
styleCell1[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
styleCell1[DocumentApp.Attribute.FOREGROUND_COLOR]='#888888';
styleCell1[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
var styleCell = {};
styleCell[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
styleCell[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
styleCell[DocumentApp.Attribute.FONT_SIZE] = 14;
styleCell[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000';
styleCell[DocumentApp.Attribute.HEIGHT] = 0.5;
styleCell[DocumentApp.Attribute.BOLD] = true;
body.removeChild(body.getChild(value));
var table = body.insertTable(value, cells);
table.getRow(0).getCell(1).appendParagraph(ttt).setHeading(DocumentApp.ParagraphHeading.HEADING3);
table.getRow(0).getCell(1).setAttributes(styleCell);
table.getRow(0).getCell(0).setWidth(2);
table.getRow(0).getCell(0).setAttributes(styleCell1);
table.setBorderColor('#ffffff');
table.getRow(0).editAsText().setBold(true);
const index = body.getChildIndex(table);
const documentId = doc.getId();
doc.saveAndClose();
const tableStart = Docs.Documents.get(documentId).body.content[index + 1].startIndex;
const tempStyle = {width: {magnitude :0, unit: "PT"}, dashStyle: "SOLID", color: {color: {rgbColor: {blue: 0}}}};
const resource = {requests: [
{updateTableCellStyle: {
tableStartLocation: {index: tableStart},
tableCellStyle: {borderTop: tempStyle, borderBottom: tempStyle, borderLeft: tempStyle, borderRight: tempStyle},
fields: "borderTop,borderBottom,borderLeft,borderRight"
}},
{updateTableCellStyle: {
tableRange: {
tableCellLocation: {tableStartLocation: {index: tableStart}, rowIndex: 0, columnIndex: 0}, rowSpan: 1, columnSpan: 1},
tableCellStyle: {
borderRight: {dashStyle: "SOLID", width: {magnitude: 3, unit: "PT"}, color: {color: {rgbColor: {red: 0.9372549019607843, green: 0.3254901960784314, blue: 0.3137254901960784}}}}
},
fields: "borderRight"
}}
]};
Docs.Documents.batchUpdate(resource, documentId);
}
function VerifTitle4(value){
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var texto = body.getChild(value);
var ttt = texto.getText();
var cells = [
['', '']
];
var styleCell1 = {};
styleCell1[DocumentApp.Attribute.FONT_SIZE] = 14;
styleCell1[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
styleCell1[DocumentApp.Attribute.FOREGROUND_COLOR]='#888888';
styleCell1[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
var styleCell = {};
styleCell[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
styleCell[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
styleCell[DocumentApp.Attribute.FONT_SIZE] = 12;
styleCell[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000';
styleCell[DocumentApp.Attribute.HEIGHT] = 0.5;
body.removeChild(body.getChild(value));
var table = body.insertTable(value, cells);
var tables = body.getTables();
table.getRow(0).getCell(1).appendParagraph(ttt).setHeading(DocumentApp.ParagraphHeading.HEADING2);
table.getRow(0).getCell(1).setAttributes(styleCell);
table.getRow(0).getCell(0).setWidth(2);
table.getRow(0).getCell(0).setAttributes(styleCell1);
table.setBorderColor('#ffffff');
table.getRow(0).editAsText().setBold(true);
const index = body.getChildIndex(table);
const documentId = doc.getId();
doc.saveAndClose();
const tableStart = Docs.Documents.get(documentId).body.content[index + 1].startIndex;
const tempStyle = {width: {magnitude :0, unit: "PT"}, dashStyle: "SOLID", color: {color: {rgbColor: {blue: 0}}}};
const resource = {requests: [
{updateTableCellStyle: {
tableStartLocation: {index: tableStart},
tableCellStyle: {borderTop: tempStyle, borderBottom: tempStyle, borderLeft: tempStyle, borderRight: tempStyle},
fields: "borderTop,borderBottom,borderLeft,borderRight"
}},
{updateTableCellStyle: {
tableRange: {
tableCellLocation: {tableStartLocation: {index: tableStart}, rowIndex: 0, columnIndex: 0}, rowSpan: 1, columnSpan: 1},
tableCellStyle: {
borderRight: {dashStyle: "SOLID", width: {magnitude: 3, unit: "PT"}, color: {color: {rgbColor: {red: 0.9372549019607843, green: 0.3254901960784314, blue: 0.3137254901960784}}}}
},
fields: "borderRight"
}}
]};
Docs.Documents.batchUpdate(resource, documentId);
var tables1 = body.getTables();
}
function VerifTextoNormal(value){
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var para = body.getParagraphs();
var styleCell = {};
styleCell[DocumentApp.Attribute.HEADING] = DocumentApp.ParagraphHeading.NORMAL;
styleCell[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
styleCell[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.JUSTIFY;
styleCell[DocumentApp.Attribute.FONT_FAMILY]='Arial';
styleCell[DocumentApp.Attribute.FONT_SIZE] = 12;
styleCell[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000';
styleCell[DocumentApp.Attribute.INDENT_FIRST_LINE] = 15;
para[value].setAttributes(styleCell);
}
How about this answer?
I could confirm the same issue using your shared sample Google Document. In this case, after updateTables()
was finished, it seems that getTables()
doesn't return the tables in the Document body. I think that this might be a bug. I thought that this might also affect to the current issue. So, in order to avoid this issue, I would like to propose to use Docs API.
At your script, when verifiStyle()
is run, it updates the tables at the last line of verifiStyle()
. This is a workaround.
When your script is modified, please modify as follows.
From:This is the script in the last line of the function verifiStyle()
.
var tables = body.getTables();
}
To:
updateTables(); // Modified
}
// Added the below function.
function updateTables() {
const docId = DocumentApp.getActiveDocument().getId();
const contents = Docs.Documents.get(docId).body.content;
const reqs = contents.reduce((ar, e) => {
if ("table" in e) {
const t = e.table.tableRows[0].tableCells;
const obj = [
{updateTextStyle: {
range: {startIndex: t[0].startIndex, endIndex: t[0].endIndex},
textStyle: {bold: true, fontSize: {magnitude: 20, unit: "PT"}, weightedFontFamily: {fontFamily: "Roboto"}},fields: "bold,fontSize,weightedFontFamily"}
},
{updateTextStyle: {
range: {startIndex: t[1].startIndex, endIndex: t[1].endIndex},
textStyle: {bold: true, fontSize: {magnitude: 18, unit: "PT"}, weightedFontFamily: {fontFamily: "Roboto"}}, fields: "bold,fontSize,weightedFontFamily"}
}
];
ar = [...ar, obj];
}
return ar;
}, []);
Docs.Documents.batchUpdate({requests: reqs}, docId);
}