Adding a Range to existing data in the row below - Apps Script

Viewed 23

I'm very new to coding and specifically to JavaScript.

Im trying to use Apps Script to code my google sheet to automate a financial spreadhseet i am making, here is my problem:

I want to input data in a single row, and have it update the row below, for example i want the range B7:M7 to be my input, so lets say for the sake of simplicity all 12 cells in that row will have the value 50

I then want it to transfer onto the range B8:M8, but if that range is already on 50, then i want it to minus that and make it 0. If range B8:M8 was on 0, then i want to make it -50. I hope this makes sense. Thank you

1 Answers

As doubleunary pointed out in the comments, your question lack info about the case, but if you use this code you can get the result you need exactly as you asked for.

please mind that here the range is fixed to work on B7:M7 to B8:M8 as you said.

You can change the getRange() numbers to work with the range you need, even with a dynamic range.

function test(){

var value1 = SpreadsheetApp.getActiveSheet().getRange(7,2,1,12).getValues();
var value2 = SpreadsheetApp.getActiveSheet().getRange(8,2,1,12).getValues();


for(var i=0;i<12;i++)
{
  value2[0][i] = value2[0][i]-value1[0][i];
}

SpreadsheetApp.getActiveSheet().getRange(8,2,1,12).setValues(value2);

}

Please, update your question as the community guidelines need.

Related