Google apps script 新行未随应用程序脚本的表属性一起提供
我试图在Google文档文件中的现有表中添加新行,但table属性未应用于新行。我可以用样式保存2个单元格。下面是应用程序脚本函数Google apps script 新行未随应用程序脚本的表属性一起提供,google-apps-script,google-docs,google-docs-api,Google Apps Script,Google Docs,Google Docs Api,我试图在Google文档文件中的现有表中添加新行,但table属性未应用于新行。我可以用样式保存2个单元格。下面是应用程序脚本函数 function addRow(fileId) { var body = DocumentApp.openById(fileId).getBody(), searchElement = body.findElement(DocumentApp.ElementType.TABLE), element = searchElemen
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);
}
以下是执行后的文档内容
正如您所看到的,即使我设置了border color属性,新行还是出现在表外。请帮助以下是您的操作方法:
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");
}
我决定使用最后一行的样式(在追加之前)作为以下行的样式,以节省时间
希望这有帮助 以下是如何做到这一点:
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");
}
我决定使用最后一行的样式(在追加之前)作为以下行的样式,以节省时间
希望这有帮助 谢谢你的评论,但是没有用。正如您在原始附件中看到的,我需要为第一个和第二个单元格设置不同的样式。当我添加日志时,lastRowAttributes也有这些值,{FONT\u SIZE=null,ITALIC=null,删除线=null,前景\u COLOR=null,最小高度=0,粗体=null,链接\u URL=null,下划线=null,FONT\u FAMILY=null,背景\u COLOR=null}@Mohammedshebin你能分享你的文档的一个清理版本吗,这样我就可以在真正的问题上测试它了?这是你好!我发现了你的问题所在!您已经设置了每个单元格的样式,而不是整个表的样式。我已经更新了我的答案,以显示如何让它在这两种情况下工作。谢谢你的评论,但它没有工作。正如您在原始附件中看到的,我需要为第一个和第二个单元格设置不同的样式。当我添加日志时,lastRowAttributes也有这些值,{FONT\u SIZE=null,ITALIC=null,删除线=null,前景\u COLOR=null,最小高度=0,粗体=null,链接\u URL=null,下划线=null,FONT\u FAMILY=null,背景\u COLOR=null}@Mohammedshebin你能分享你的文档的一个清理版本吗,这样我就可以在真正的问题上测试它了?这是你好!我发现了你的问题所在!您已经设置了每个单元格的样式,而不是整个表的样式。我已经更新了我的答案,以说明如何使它在这两种情况下工作。