使用 Google Apps 脚本为 google 文档 table 自定义边框
Customized border for google document table with Google Apps Script
我对 Google 的应用程序脚本还很陌生。我已经阅读了文档,我已经可以做一些事情,但是我在解决问题时遇到了一些困难。
我正在使用文档,我需要在我的文档中添加 select 文本并将其放在 table 中。这个table只有一行两列。 selected 文本必须在该行的第二个单元格中,稍后,我需要删除 selected 文本并仅保留 table 文本。
select 将文本放置在 table 上的部分,我已经可以做到了,现在我发现很难使用 table 布局,特别是在与table的边框部分有关。见下图。
我的table只需要让中心边框可见且为红色,其他边框必须隐藏。
我没有在文档的文档中找到任何与单独操作边框有关的内容,而只是对整个 table 进行一般操作。
下面的代码做了很多工作,但我想要的样式边缘,还没有。
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 列的 table 的中心设置垂直边框。
- 例如,边框样式宽度为3磅,颜色为红色。
- 您想使用 Google Apps 脚本实现此目的。
问题和解决方法:
不幸的是,似乎没有方法可以使用 Document service 将边框仅赋予 table 的中心。所以在这个答案中,作为一种解决方法,我想建议使用 Google Docs API。当Docs的batchUpdate方法API,你的目的就可以达到
在这个答案中,在使用您的脚本插入 table 之后,插入的 table 的起始索引由 Docs API 的 get 方法检索,然后, table 单元格样式被 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);
参考文献:
我对 Google 的应用程序脚本还很陌生。我已经阅读了文档,我已经可以做一些事情,但是我在解决问题时遇到了一些困难。 我正在使用文档,我需要在我的文档中添加 select 文本并将其放在 table 中。这个table只有一行两列。 selected 文本必须在该行的第二个单元格中,稍后,我需要删除 selected 文本并仅保留 table 文本。 select 将文本放置在 table 上的部分,我已经可以做到了,现在我发现很难使用 table 布局,特别是在与table的边框部分有关。见下图。
我的table只需要让中心边框可见且为红色,其他边框必须隐藏。 我没有在文档的文档中找到任何与单独操作边框有关的内容,而只是对整个 table 进行一般操作。 下面的代码做了很多工作,但我想要的样式边缘,还没有。
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 列的 table 的中心设置垂直边框。
- 例如,边框样式宽度为3磅,颜色为红色。
- 您想使用 Google Apps 脚本实现此目的。
问题和解决方法:
不幸的是,似乎没有方法可以使用 Document service 将边框仅赋予 table 的中心。所以在这个答案中,作为一种解决方法,我想建议使用 Google Docs API。当Docs的batchUpdate方法API,你的目的就可以达到
在这个答案中,在使用您的脚本插入 table 之后,插入的 table 的起始索引由 Docs API 的 get 方法检索,然后, table 单元格样式被 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);