2016-04-29 99 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」,這是真的!所以我們最好使用字符串比較。 –