2016-04-29 94 views
3

我知道使用这些功能,我可以显示和隐藏列:谷歌电子表格脚本“切换”隐藏/视图行/列

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个按钮或菜单项的情况下激活隐藏/显示功能。

回答

3

我没有看到用脚本检测列的隐藏/未隐藏状态的方法,但切换行为可以通过将状态存储在Script Properties中来实现。这是管理属性并根据需要调用其他两个函数中的任何一个的函数。

function toggleColumns() { 
    var scriptProperties = PropertiesService.getScriptProperties(); 
    if (scriptProperties.getProperty('hidden')) { 
    showColumns(); 
    scriptProperties.setProperty('hidden', false); 
    } 
    else { 
    hideColumns(); 
    scriptProperties.setProperty('hidden', true); 
    } 
} 
+0

这不起作用,因为所有属性都以字符串形式存储,即false被存储为“false”,这是真的!所以我们最好使用字符串比较。 –

相关问题