如何唯一标识已插入的表?
How do I uniquely identify tables that I have inserted?
我需要通过脚本将一些 table 插入到 Google 文档中,然后仅定期更新我通过脚本插入的 table。我不想修改没有脚本插入的其他 tables...只需更改脚本插入的那些。但是,我看不到为每个 table 放置属性或唯一名称的方法。有办法吗?
var table = body.insertTable(insertPoint, [[row.join("")]]);
我相信你的目标如下。
- 您想使用 Google Apps 脚本检索特定的 table。
- 您想使用
var table = body.insertTable(insertPoint, [[row.join("")]]);
放置 table。
- table 的位置和顺序已更改。
为此,这个答案怎么样?
问题和解决方法:
很遗憾,在当前阶段,Google 文档中的 table 不能具有唯一 ID。通过这种方式,无法直接检索特定的table。对于您的情况,为了实现上述目标,在此答案中,我想提出以下解决方法。
- 当插入table时,在table的上方添加一段文字作为唯一ID。通过将字体颜色修改为与背景颜色相同并将字体大小修改为1,显然隐藏了唯一ID。
- 检索到 table 时,将使用唯一 ID 检索特定的 table。
示例脚本:
插入一个table:
function putTable() {
const documentId = "###"; // Please set Document ID.
const uniqueIdOfTable = "tableId1"; // Please set the unique table ID. In this sample, "tableId1" is used.
const insertPoint = 1; // In this sample, "1" is the minumum size.
const row = ["a1", "b1", "c1"];
const body = DocumentApp.openById(documentId).getBody();
const table = body.insertTable(insertPoint, [[row.join("")]]);
const text = table.getPreviousSibling().asParagraph().appendText("\n" + uniqueIdOfTable);
text.setFontSize(1).setForegroundColor(text.getBackgroundColor() || "#ffffff");
}
- 当您运行此脚本时,会插入一个table并添加
tableId1
的唯一ID。唯一ID被字体大小和字体颜色隐藏了。
使用唯一 ID 检索特定 table:
function getTable(documentId, uniqueIdOfTable) {
const body = DocumentApp.openById(documentId).getBody();
const index = body.getChildIndex(body.findText(uniqueIdOfTable).getElement().getParent());
const element = body.getChild(index + 1);
return element.getType() === DocumentApp.ElementType.TABLE ? element.asTable() : null;
}
// Please run this function.
function main() {
const documentId = "###"; // Please set Document ID.
const uniqueIdOfTable = "tableId1"; // Please set the unique table ID. In this sample, "tableId1" is used.
const table = getTable(documentId, uniqueIdOfTable);
console.log(table.editAsText().getText())
}
- 当您 运行
main()
时,使用 tableId1
的唯一 ID 检索 table。在这种情况下,getTable()
returns table 对象。
注:
- 这是一个简单的示例脚本,用于解释解决方法。所以请根据您的实际情况进行修改。
我需要通过脚本将一些 table 插入到 Google 文档中,然后仅定期更新我通过脚本插入的 table。我不想修改没有脚本插入的其他 tables...只需更改脚本插入的那些。但是,我看不到为每个 table 放置属性或唯一名称的方法。有办法吗?
var table = body.insertTable(insertPoint, [[row.join("")]]);
我相信你的目标如下。
- 您想使用 Google Apps 脚本检索特定的 table。
- 您想使用
var table = body.insertTable(insertPoint, [[row.join("")]]);
放置 table。 - table 的位置和顺序已更改。
为此,这个答案怎么样?
问题和解决方法:
很遗憾,在当前阶段,Google 文档中的 table 不能具有唯一 ID。通过这种方式,无法直接检索特定的table。对于您的情况,为了实现上述目标,在此答案中,我想提出以下解决方法。
- 当插入table时,在table的上方添加一段文字作为唯一ID。通过将字体颜色修改为与背景颜色相同并将字体大小修改为1,显然隐藏了唯一ID。
- 检索到 table 时,将使用唯一 ID 检索特定的 table。
示例脚本:
插入一个table:
function putTable() {
const documentId = "###"; // Please set Document ID.
const uniqueIdOfTable = "tableId1"; // Please set the unique table ID. In this sample, "tableId1" is used.
const insertPoint = 1; // In this sample, "1" is the minumum size.
const row = ["a1", "b1", "c1"];
const body = DocumentApp.openById(documentId).getBody();
const table = body.insertTable(insertPoint, [[row.join("")]]);
const text = table.getPreviousSibling().asParagraph().appendText("\n" + uniqueIdOfTable);
text.setFontSize(1).setForegroundColor(text.getBackgroundColor() || "#ffffff");
}
- 当您运行此脚本时,会插入一个table并添加
tableId1
的唯一ID。唯一ID被字体大小和字体颜色隐藏了。
使用唯一 ID 检索特定 table:
function getTable(documentId, uniqueIdOfTable) {
const body = DocumentApp.openById(documentId).getBody();
const index = body.getChildIndex(body.findText(uniqueIdOfTable).getElement().getParent());
const element = body.getChild(index + 1);
return element.getType() === DocumentApp.ElementType.TABLE ? element.asTable() : null;
}
// Please run this function.
function main() {
const documentId = "###"; // Please set Document ID.
const uniqueIdOfTable = "tableId1"; // Please set the unique table ID. In this sample, "tableId1" is used.
const table = getTable(documentId, uniqueIdOfTable);
console.log(table.editAsText().getText())
}
- 当您 运行
main()
时,使用tableId1
的唯一 ID 检索 table。在这种情况下,getTable()
returns table 对象。
注:
- 这是一个简单的示例脚本,用于解释解决方法。所以请根据您的实际情况进行修改。