Leo*_*lva 3 javascript google-docs google-docs-api google-apps-script
我对 Google 的应用程序脚本非常陌生。我已阅读文档,并且已经可以执行一些操作,但在解决问题时遇到了一些困难。\n我正在使用文档,我需要在文档中选择文本并将其放入表格中。该表只有一行和两列。所选文本必须位于该行的第二个单元格中,稍后,我需要删除所选文本,只保留带有文本的表格。\n选择文本以将其放置在表格上的部分,我已经可以做到,现在我发现很难使用表格布局,特别是与表格边框部分相关的部分。请参见下图。
\n\n\n\n我的表格只需要中央边框可见并为红色,其他边框必须隐藏。\n我在文档的文档中没有找到与单独操作边框无关的内容,但仅适用于整个表格.\n下面的代码做了很多工作,但还没有达到我想要的样式的边缘。
\n\nfunction myFunction() {\r\n var doc = DocumentApp.getActiveDocument();\r\n var body = doc.getBody();\r\n var selection = doc.getSelection();\r\n var ui = DocumentApp.getUi();\r\n var text = body.editAsText();\r\n var report = "";\r\n //----------------------------------------------------------------------------------------------------------\\\\\r\n \r\n if (!selection) {\r\n report += " Nenhuma sele\xc3\xa7\xc3\xa3o atual ";\r\n ui.alert( report );\r\n }\r\n else{\r\n var elements = selection.getSelectedElements();\r\n var element = elements[0].getElement();\r\n var selectedText = element.asText().getText();\r\n \r\n var styleCell1 = {};\r\n styleCell1[DocumentApp.Attribute.FONT_SIZE] = 20;\r\n styleCell1[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;\r\n styleCell1[DocumentApp.Attribute.FOREGROUND_COLOR]=\'#888888\';\r\n styleCell1[DocumentApp.Attribute.FONT_FAMILY]=\'Roboto\';\r\n \r\n var styleCell = {};\r\n styleCell[DocumentApp.Attribute.FONT_SIZE] = 18;\r\n styleCell1[DocumentApp.Attribute.FOREGROUND_COLOR]=\'#000000\';\r\n styleCell[DocumentApp.Attribute.HEADING] = DocumentApp.ParagraphHeading.HEADING1;\r\n styleCell[DocumentApp.Attribute.FONT_FAMILY]=\'Roboto\';\r\n \r\n var cells = [\r\n [\'\', \'\']\r\n ];\r\n \r\n //body.insertParagraph(0, doc.getName()).setHeading(DocumentApp.ParagraphHeading.HEADING1);\r\n table = body.appendTable(cells);\r\n\r\n table.getRow(0).editAsText().setBold(true);\r\n table.getRow(0).getCell(1).setText(selectedText);\r\n table.getRow(0).getCell(1).setAttributes(styleCell);\r\n table.getRow(0).getCell(0).setWidth(59);\r\n table.getRow(0).getCell(0).setAttributes(styleCell1);\r\n table.setBorderColor(\'#ffffff\');\r\n table.setBorderWidth(3);\r\n }\r\n}
Run Code Online (Sandbox Code Playgroud)\r\n不幸的是,似乎没有方法可以使用文档服务仅将边框赋予表格的中心。因此,在这个答案中,作为一种解决方法,我想建议使用 Google Docs API。当使用Docs API的batchUpdate方法时,你的目标就可以达到。
在此答案中,使用脚本插入表格后,通过 Docs API 的 get 方法检索插入表格的起始索引,然后通过 Docs API 的 batchUpdate 方法修改表格单元格样式。
当您的脚本修改时,请修改如下。在运行脚本之前,请在高级 Google 服务中启用 Google Docs API。
table.setBorderWidth(3);
Run Code Online (Sandbox Code Playgroud)
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: 1}}}}
},
fields: "borderRight"
}}
]};
Docs.Documents.batchUpdate(resource, documentId);
Run Code Online (Sandbox Code Playgroud)