我知道使用这些函数我可以显示和隐藏列:
function showColumns() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
sheet.showColumns(2,3); // B-D, three columns starting from 2nd
sheet.showColumn(7); // G, column number 7
}
function hideColumns() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
sheet.hideColumns(2,3); // B-D, three columns starting from 2nd
sheet.hideColumn(7); // G, column 7
}
但为此,我需要两个按钮或菜单脚本,一个用于隐藏列,一个用于显示列。
我想知道是否存在“切换”功能,这样我就可以在不需要 2 个按钮或菜单项的情况下激活隐藏/显示功能。
最佳答案
我没有看到用脚本检测列的隐藏/未隐藏状态的方法,但可以通过将状态存储在 Script Properties 中来实现切换行为。 .这是管理属性并根据需要调用其他两个函数之一的函数。
function toggleColumns() {
var scriptProperties = PropertiesService.getScriptProperties();
if (scriptProperties.getProperty('hidden')) {
showColumns();
scriptProperties.setProperty('hidden', false);
}
else {
hideColumns();
scriptProperties.setProperty('hidden', true);
}
}
关于google-apps-script - Google 电子表格脚本 "toggle"隐藏/查看列/行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36945612/