Apps 脚本的新行不带有 table 属性
New row not coming with table properties with Apps script
我试图在 Google 文档文件中的现有 table 中添加新行,但 table 属性 未应用于新行。我能够通过样式保存 2 个单元格。这是 Apps 脚本函数
function addRow(fileId)
{
var body = DocumentApp.openById(fileId).getBody(),
searchElement = body.findElement(DocumentApp.ElementType.TABLE),
element = searchElement.getElement(),
table = element.asTable();
var cell1Style = table.getRow(0).getCell(0).getAttributes();
var cell2Style = table.getRow(0).getCell(1).getAttributes();
cell1Style[DocumentApp.Attribute.BACKGROUND_COLOR] = '#ffffff';
cell2Style[DocumentApp.Attribute.BACKGROUND_COLOR] = '#ffffff';
var rowStyle = {};
rowStyle[DocumentApp.Attribute.BORDER_COLOR] = '#000000';
var tr = table.appendTableRow();
tr.setAttributes(rowStyle);
tr.appendTableCell('My Text:').setAttributes(cell1Style);
tr.appendTableCell('My value').setAttributes(cell2Style);
}
执行后的文档内容如下
如您所见,新行出现在 table 之外,即使我已经设置了边框颜色属性。请帮忙
以下是操作方法:
function myFunction() {
var body = DocumentApp.getActiveDocument().getBody();
var table = body.findElement(DocumentApp.ElementType.TABLE).getElement().asTable();
var newRow = table.appendTableRow();
var lastRowAttributes = table.getRow(table.getNumRows()-1).getAttributes()
newRow.appendTableCell("Text 1").setAttributes(lastRowAttributes);
newRow.appendTableCell("Text 2").setAttributes(lastRowAttributes);
}
如果这对您不起作用,您可以使用:
function myFunction() {
var body = DocumentApp.getActiveDocument().getBody();
var table = body.findElement(DocumentApp.ElementType.TABLE).getElement().asTable();
var lastRow = table.getRow(table.getNumRows()-1);
var newRow = table.appendTableRow(lastRow.copy());
newRow.getCell(0).setText("Text 1");
newRow.getCell(1).setText("Text 2");
}
我决定使用最后一行(附加之前)的样式作为后续行的样式以节省时间。
希望对您有所帮助!
我试图在 Google 文档文件中的现有 table 中添加新行,但 table 属性 未应用于新行。我能够通过样式保存 2 个单元格。这是 Apps 脚本函数
function addRow(fileId)
{
var body = DocumentApp.openById(fileId).getBody(),
searchElement = body.findElement(DocumentApp.ElementType.TABLE),
element = searchElement.getElement(),
table = element.asTable();
var cell1Style = table.getRow(0).getCell(0).getAttributes();
var cell2Style = table.getRow(0).getCell(1).getAttributes();
cell1Style[DocumentApp.Attribute.BACKGROUND_COLOR] = '#ffffff';
cell2Style[DocumentApp.Attribute.BACKGROUND_COLOR] = '#ffffff';
var rowStyle = {};
rowStyle[DocumentApp.Attribute.BORDER_COLOR] = '#000000';
var tr = table.appendTableRow();
tr.setAttributes(rowStyle);
tr.appendTableCell('My Text:').setAttributes(cell1Style);
tr.appendTableCell('My value').setAttributes(cell2Style);
}
执行后的文档内容如下
如您所见,新行出现在 table 之外,即使我已经设置了边框颜色属性。请帮忙
以下是操作方法:
function myFunction() {
var body = DocumentApp.getActiveDocument().getBody();
var table = body.findElement(DocumentApp.ElementType.TABLE).getElement().asTable();
var newRow = table.appendTableRow();
var lastRowAttributes = table.getRow(table.getNumRows()-1).getAttributes()
newRow.appendTableCell("Text 1").setAttributes(lastRowAttributes);
newRow.appendTableCell("Text 2").setAttributes(lastRowAttributes);
}
如果这对您不起作用,您可以使用:
function myFunction() {
var body = DocumentApp.getActiveDocument().getBody();
var table = body.findElement(DocumentApp.ElementType.TABLE).getElement().asTable();
var lastRow = table.getRow(table.getNumRows()-1);
var newRow = table.appendTableRow(lastRow.copy());
newRow.getCell(0).setText("Text 1");
newRow.getCell(1).setText("Text 2");
}
我决定使用最后一行(附加之前)的样式作为后续行的样式以节省时间。
希望对您有所帮助!