点击

时间:2016-08-20 12:55:32

标签: google-apps-script

我已经有了简单的谷歌电子表格脚本,当我在触发器单元格中选择不同的值时,它会隐藏列和行。

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等)中的行:if&#34 ; C"此行中的单元格为空或等于0.

我尝试过搜索但不知道如何实现我找到的代码。

你能帮忙吗?

1 个答案:

答案 0 :(得分: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;
}

我们还有一行隐藏。喜欢这个

/*
 * 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);
}