2016-08-20 53 views
0

我已經有簡單的谷歌電子表格腳本隱藏列和行時,我在觸發單元格中選擇不同的值。用點擊標準隱藏行

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 
} 

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 onEdit(e) { 

    var sheet = e.source.getActiveSheet(); 
    if (e.range.getA1Notation() !== 'B2' || sheet.getName() !== 'sheet1') 
     return; 
    switch (e.value) { 
     case '01': 
      sheet.showColumns(1, sheet.getMaxColumns() - 1) 
      sheet.showRows(1, sheet.getMaxRows() - 1) 
      break; 
     case '02': 
      sheet.showColumns(1, sheet.getMaxColumns() - 1) 
      sheet.showRows(1, sheet.getMaxRows() - 1) 
      sheet.hideColumns(6, 4) 
      sheet.hideRows(12, 1) 
      break; 
     case '03': 
      sheet.showColumns(1, sheet.getMaxColumns() - 1) 
      sheet.showRows(1, sheet.getMaxRows() - 1) 
      sheet.hideColumns(4, 2) 
      sheet.hideColumns(8, 2) 
      sheet.hideRows(12, 1) 
      sheet.hideRows(7, 3) 
      break; 
     case '04': 
      sheet.showColumns(1, sheet.getMaxColumns() - 1) 
      sheet.showRows(1, sheet.getMaxRows() - 1) 
      sheet.hideColumns(4, 4) 
      sheet.hideRows(12, 1) 
      break; 
    } 
} 

現在我想添加隱藏在四個不同的範圍不僅確切行也行能力(如20-40,30-35,50-60等)符合條件:如果「 C「的單元格是空白或等於0.

我試過搜索但不知道如何實現我找到的代碼。

你能幫忙嗎?

+0

現在我需要將所有行都隱藏在一種特定顏色中。我用條件格式做了它。所以基本上現在我可以用這種顏色隱藏所有的行。 –

+0

Rodion Zhabrev您不遵循社區規則 – oshliaer

+0

已更新https://plus.google.com/u/0/communities/103356854721490738172 – oshliaer

回答

0

好的。你的方法並不全面。我認爲。

假設我們有一個過濾器函數,它在沒事的時候返回true。

/* 
* v is value of a cell 
* i is current row - 1 
* j is current column -1 
*/ 
function filter(v, i, j) { 
    return v !== '#ff0000' && j == 2; 
} 

此外,我們有一個行hider。像這樣

/* 
* blocks is 2D array. It contains pairs of rows indexes and counts rows for hiding. 
* [[1, 2],[5, 4]] This hides from first row two items, from fifth row four items. 
*/ 
function hiderRows(sheet, blocks) { 
    for (var i = 0; i < blocks.length; i++) { 
     sheet.hideRows(blocks[i][0], blocks[i][1]); 
    } 
    return sheet; 
} 

我們需要一個塊生成器。我們開始做吧!

function blockBilder(sheet, callback, colors) { 
    var cmd = colors ? 'getBackgrounds' : 'getValues'; 
    var values = sheet.getDataRange()[cmd](); 
    var blocks = []; 
    var delta = 0; 
    for (var i = 0; i < values.length; i++) { 
     for (var j = 0; j < values[i].length; j++) { 
      if (callback(values[i][j], i, j)) { 
       if (delta === i - 1) { 
        blocks[blocks.length - 1][1]++; 
       } else { 
        blocks.push([i + 1, 1]); 
       } 
       delta = i; 
      } 
     } 
    } 
    return blocks; 
} 

運行它!

function test() { 
    var sheet = SpreadsheetApp.getActiveSheet(); 
    sheet.showRows(1, sheet.getLastRow() - 1); 
    var blocks = blockBilder(sheet, filter, 'any value here'); 
    hiderRows(sheet, blocks); 
}