Spreadsheet Non-Adjacent Column data

Viewed 2761

I want data from a spreadsheet using app script. I am trying to get data from multipe rows of two non-adjacent colums, but I am unable to do so using following syntax. Say A1 to A19 and C1 to C19. Please tell me if something is wrong with the code. Thanks.

data = sheet.getRange("A1:A19","C1:C19").getValues();

4 Answers

You can get all range and remove the columns you don't need. Addapted from Alex Wayne (Deleting a column from a multidimensional array in javascript)

var table = [
    ['a', 'b', '1', '5', '8'],
    ['c', 'd', '2', '6', '9'],
    ['f', 'g', '3', '7', '10']
];

var removeCol = function(arr, col1Index, col2Index) {
    for (var i = 0; i < arr.length; i++) {
        var row = arr[i];
        row.splice(col1Index, 1);
        row.splice(col2Index, 1);
    }
}

// remove more than one column - can remove as many columns as needed
// ATENTION: spreadsheet column start in 1 and arrays start in 0
// start removing from last to first columns
removeCol(table, 4, 2);

alert(table);
Related