在這個基本示例中,我試圖查看在 google 檔案中找到的表的每行中有多少列。
tables.forEach(function(table) {
var numberOfRows = table.getNumRows();
console.log("New Table with Rows: " numberOfRows);
for (var rowIndex = 0; rowIndex < numberOfRows; rowIndex ) {
var row = table.getRow(rowIndex);
console.log("row: " rowIndex " with num of cells: " row.getNumCells());
}
})
這是我的表作為測驗的樣子。

這是我的代碼的輸出剪斷
New Table with Rows: 6
row: 0 with num of cells: 4
row: 1 with num of cells: 4
row: 2 with num of cells: 4
row: 3 with num of cells: 4
row: 4 with num of cells: 4
row: 5 with num of cells: 4
我期望它輸出的單元格數是每個特定行中的列數。我錯過了什么?似乎 getNumCells 回傳整個表中的最大列數。如何計算每行有多少列?
uj5u.com熱心網友回復:
在你的情況下,使用getColSpan()Class TableCell的方法怎么樣?當這反映到您的腳本時,它變成如下。
修改后的腳本:
tables.forEach(function(table) {
var numberOfRows = table.getNumRows();
console.log("New Table with Rows: " numberOfRows);
for (var rowIndex = 0; rowIndex < numberOfRows; rowIndex ) {
var row = table.getRow(rowIndex);
var nCol = 0;
for (var colIndex = 0; colIndex < row.getNumCells(); colIndex ) {
nCol = row.getCell(colIndex).getColSpan() == 0 ? 0 : 1;
}
console.log("row: " rowIndex " with num of cells: " nCol);
}
});
參考:
- getColSpan()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/381019.html
