JonK
JonK

Reputation: 49

How can I skip a column when using getRange() - Google Apps Script

I cant seem to figure out how to put this script to skip column L. I tried many different varieties but all result in error. I hope somebody with more experience can shed light. I need I:K and M:O without L.

sheet1.getRange("I1:O"+sheet1.getLastRow()).getValues()

Upvotes: 3

Views: 1928

Answers (2)

Marios
Marios

Reputation: 27350

Another approach:

const array = sheet1.getRange('I1:O'+sheet1.getLastRow()).getValues();
array.forEach(a => a.splice(3, 1));

In more detail, 3 is chosen because, starting from 0, L is at the third position: I, J, K, L.

Code snippet:

function myFunction() {
  const ss = SpreadsheetApp.getActive();
  const sheet1 = ss.getSheetByName('Sheet1');
  const array = sheet1.getRange('I1:O'+sheet1.getLastRow()).getValues();
  array.forEach(a => a.splice(3, 1));
  console.log(array);
}

Upvotes: 2

Tanaike
Tanaike

Reputation: 201428

As a simpl modification, how about the following modification?

From:

sheet1.getRange("I1:O"+sheet1.getLastRow()).getValues()

To:

sheet1.getRange("I1:O"+sheet1.getLastRow()).getValues().map(([i,j,k,,m,n,o]) => [i,j,k,m,n,o]);

or

sheet1.getRange("I1:O"+sheet1.getLastRow()).getValues().map(([i,j,k,,...mno]) => [i,j,k,...mno]);

Note:

  • Please use this modified script with enabling V8 runtime.

References:

Upvotes: 4

Related Questions