我正在使用多个 Google 表格,每个文件有多个电子表格。我需要编写一个 API 代码,自动隐藏仅在活动电子表格中没有数据的所有列(而不是在该文件中的每个工作表中)。
我一直在看这篇文章:当单元格在谷歌脚本中为空时隐藏列以获取指导,但尚未成功地根据我的目的进行调整。
我将非常感谢您的帮助,因为我是一个完全的新手。
隐藏没有数据的列
function hideEmptyColumns() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const hr = 1;//header row
[... new Array(sh.getMaxColumns()).keys()].forEach(idx => {
if (getColumnHeight1(idx + 1, sh, ss) < (hr + 1)) {
sh.hideColumns(idx + 1);
}
});
}
function getColumnHeight1(col, sh, ss) {
var ss = ss || SpreadsheetApp.getActive();
var sh = sh || ss.getActiveSheet();
var col = col || sh.getActiveCell().getColumn();
var rcA = [];
if (sh.getLastRow()) { rcA = sh.getRange(1, col, sh.getLastRow(), 1).getValues().flat().reverse(); }
let s = 0;
for (let i = 0; i < rcA.length; i++) {
if (rcA[i].toString().length == 0) {
s++;
} else {
break;
}
}
return rcA.length - s;
}