Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 540
JonK Avatar asked Feb 21 '26 19:02

JonK


2 Answers

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:

  • map()
  • Destructuring assignment
  • Spread syntax (...)
like image 138
Tanaike Avatar answered Feb 23 '26 08:02

Tanaike


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);
}
like image 24
soMarios Avatar answered Feb 23 '26 09:02

soMarios



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!