我正在尝试为 Google 电子表格编写一个函数,用于定位组的第一列和最后一列。然后它会隐藏该组,或者如果它已经隐藏则显示它。
但是,我还没有找到一种方法来确定一列是否隐藏。我在 Google 的课程表页面https://developers.google.com/apps-script/reference/spreadsheet/sheet上找不到任何东西,而且我还没有找到与 Excel 的 .hidden 等效的东西
getColumnWidth(column) 返回未隐藏的列宽,即使它被隐藏。
这是我的代码:
function hideShowColumns(startCol, endCol) {
//endCol is one column past the last data set that should be hidden
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Transposed Data");
//import the data from the Column Headers
var colHeaderData = sheet.getRange(1, 1, 2, sheet.getMaxColumns()).getValues();
var startColNum = 0;
var endColNum = 0;
// This section searches for the column names in the header rows and returns their column number
for (var i =0; i < 2; ++i) {
for (var j = 0; j < colHeaderData[0].length; ++j) {
if (colHeaderData[i][j] == startCol)
startColNum = j;
if (colHeaderData[i][j] == endCol)
endColNum = j;
}
}
//This is the wrong command getColumnWidth doesn't change if column is hidden
if (sheet.getColumnWidth(startColNum + 1) != 0) {
sheet.hideColumns(startColNum + 2, endColNum - startColNum - 1);
Logger.log(sheet.getColumnWidth(startColNum + 2));
return;
}
//This is the wrong command getColumnWidth doesn't change if column is hidden
if (sheet.getColumnWidth(startColNum + 1) == 0) {
for (var j = startColNum + 1; j < endColNum - 1; ++j) {
sheet.unhideColumn(j);
Logger.log(sheet.getColumnWidth(startColNum + 2));
}
return;
}
}
谢谢您的帮助!