引用 Google 文档表格中的单元格
Referencing cells in tables on a Google Doc
我正在尝试在 Google 文档单元格中逐个自动格式化 table。一旦我将 table 存储在变量 table
中,我尝试使用 getCell
来引用此 table 左上角的单元格,但它不会 return我要找的小区
我注意到在 Google 脚本参考中,他们指出要指向一个单元格,正确的用法是 getCell(rowIndex, cellIndex)
。 cellIndex
在这种情况下指的是什么,它是否与某种 columnIndex
相关?
如有任何建议,我们将不胜感激。
是的,cellIndex指的是columnIndex。考虑下面的例子
function getCellVal() {
var docBody=DocumentApp.getActiveDocument().getBody();
var table=docBody.getTables()[0];
Logger.log(table.getCell(0,0).getText())
}
此处,Logger 中的值将检索 table 第一行第一列的单元格值。行和列的索引从 0 开始。
希望对您有所帮助!
在 Google 文档中编辑 table
这是一个示例,它通过将光标放在其中一个单元格内来编辑 table 的所有单元格内容。
rowIndex 从 0 变为 table.getNumRows()-1;
cellIndex 从 0 变为 table.getRow(i).getNumCells()-1
function editTableAtCursor(){
var doc=DocumentApp.getActiveDocument();
var el=doc.getCursor().getElement().getParent().getParent().getParent();
var table=el.asTable();
var rows=table.getNumRows();
for(var i=0;i<table.getNumRows();i++) {
for(var j=0;j<table.getRow(i).getNumCells();j++) {
table.getCell(i,j).editAsText().setText(Utilities.formatString('Row: %s Cell: %s',i,j));
}
}
}
- Table Class
- Example that replaces table with an Array
我正在尝试在 Google 文档单元格中逐个自动格式化 table。一旦我将 table 存储在变量 table
中,我尝试使用 getCell
来引用此 table 左上角的单元格,但它不会 return我要找的小区
我注意到在 Google 脚本参考中,他们指出要指向一个单元格,正确的用法是 getCell(rowIndex, cellIndex)
。 cellIndex
在这种情况下指的是什么,它是否与某种 columnIndex
相关?
如有任何建议,我们将不胜感激。
是的,cellIndex指的是columnIndex。考虑下面的例子
function getCellVal() {
var docBody=DocumentApp.getActiveDocument().getBody();
var table=docBody.getTables()[0];
Logger.log(table.getCell(0,0).getText())
}
此处,Logger 中的值将检索 table 第一行第一列的单元格值。行和列的索引从 0 开始。
希望对您有所帮助!
在 Google 文档中编辑 table
这是一个示例,它通过将光标放在其中一个单元格内来编辑 table 的所有单元格内容。
rowIndex 从 0 变为 table.getNumRows()-1;
cellIndex 从 0 变为 table.getRow(i).getNumCells()-1
function editTableAtCursor(){
var doc=DocumentApp.getActiveDocument();
var el=doc.getCursor().getElement().getParent().getParent().getParent();
var table=el.asTable();
var rows=table.getNumRows();
for(var i=0;i<table.getNumRows();i++) {
for(var j=0;j<table.getRow(i).getNumCells();j++) {
table.getCell(i,j).editAsText().setText(Utilities.formatString('Row: %s Cell: %s',i,j));
}
}
}
- Table Class
- Example that replaces table with an Array