我对 Google 的应用程序脚本还很陌生。我已经阅读了文档,我已经可以做一些事情,但是我在解决问题时遇到了一些困难。 我正在使用文档,我需要在我的文档中选择文本并将其放在表格中。该表只有一行两列。选定的文本必须在该行的第二个单元格中,稍后,我需要删除选定的文本并只保留带有文本的表格。 选择文本将其放置在表格上的部分,我已经可以做到,现在我发现很难使用表格布局,特别是与表格边框部分相关的部分。请参见下图。
我的表格只需要中央边框可见且为红色,其他边框必须隐藏。 我没有在文档的文档中找到任何与单独操作边框相关的内容,但只是对整个表格进行一般操作。 下面的代码做了很多工作,但我想要的样式边缘,还没有。
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var selection = doc.getSelection();
var ui = DocumentApp.getUi();
var text = body.editAsText();
var report = "";
//----------------------------------------------------------------------------------------------------------\\
if (!selection) {
report += " Nenhuma seleção atual ";
ui.alert( report );
}
else{
var elements = selection.getSelectedElements();
var element = elements[0].getElement();
var selectedText = element.asText().getText();
var styleCell1 = {};
styleCell1[DocumentApp.Attribute.FONT_SIZE] = 20;
styleCell1[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
styleCell1[DocumentApp.Attribute.FOREGROUND_COLOR]='#888888';
styleCell1[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
var styleCell = {};
styleCell[DocumentApp.Attribute.FONT_SIZE] = 18;
styleCell1[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000';
styleCell[DocumentApp.Attribute.HEADING] = DocumentApp.ParagraphHeading.HEADING1;
styleCell[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
var cells = [
['', '']
];
//body.insertParagraph(0, doc.getName()).setHeading(DocumentApp.ParagraphHeading.HEADING1);
table = body.appendTable(cells);
table.getRow(0).editAsText().setBold(true);
table.getRow(0).getCell(1).setText(selectedText);
table.getRow(0).getCell(1).setAttributes(styleCell);
table.getRow(0).getCell(0).setWidth(59);
table.getRow(0).getCell(0).setAttributes(styleCell1);
table.setBorderColor('#ffffff');
table.setBorderWidth(3);
}
}
最佳答案
- 您只想为具有 1 行和 2 列的表格的中心提供垂直边框。
- 例如,边框样式为宽度为3磅,颜色为红色。
- 您想使用 Google Apps 脚本实现这一目标。
问题和解决方法:
不幸的是,似乎没有使用 Document service 将边框仅赋予表格中心的方法。 .所以在这个答案中,作为一种解决方法,我想建议使用 Google Docs API。当Docs API的batchUpdate方法时,你的目的就可以达到。
在这个答案中,在使用您的脚本插入表格后,通过 Docs API 的 get 方法检索插入表格的起始索引,然后通过 Docs API 的 batchUpdate 方法修改表格单元格样式。
修改后的脚本:
当您的脚本修改时,请修改如下。在运行脚本之前,please enable Google Docs API at Advanced Google services.
来自:
table.setBorderWidth(3);
收件人:
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);
引用资料:
关于javascript - 使用 Google Apps 脚本为 google 文档表自定义边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61264280/