I have a range consisting of a variable number of rows and 4 columns (A,B,C,D). I need to loop through each of the rows and add the number in column D to the number in column C and then clear the contents of column D. I think I can get the adding and the clearing if i can get the correct cells referenced. i was wanting to use a for or foreach loop and then offset from column A to columns C & D but have been unable to get the offset to work.
function AddInventory2() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Total Inventory')
const lrow = sh.getLastRow();
var Avals = sh.getRange("A1:A"+lrow).getValues();
var Alast = lrow - Avals.reverse().findIndex(c=>c[0]!='');
Avals.forEach(function(row){
targetCell = Avals.offset(0,3);
Logger.log(targetCell);
})
}
Using offset within a for loop:
The code below achieved the desired requirement of offsetting to columns C & D from each of the cells in column A that contain a value.
Note: However, I wouldn't suggest using offset in a loop if you have more complex data, as it can increase the number of calls to the Spreadsheet service, which may slow down execution and cause runtime issues.
Code:
function AddInventory2() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh = ss.getSheetByName('Total Inventory');
const lrow = sh.getLastRow();
for (let i = 1; i <= lrow; i++) {
const cellA = sh.getRange(i, 1);
const cellC = cellA.offset(0, 2);
const cellD = cellA.offset(0, 3);
const valueA = cellA.getValue();
const valueC = cellC.getValue();
const valueD = cellD.getValue();
if (valueA !== "") {
cellD.setValue(valueA);
cellC.setValue(valueC + valueA);
cellA.clear();
cellD.clear();
}
}
}
Alternative way for a more complex dataset:
In the future, if you have a large dataset, this approach is much better than the first one. It handles large datasets more efficiently by processing the data in memory
and reducing the number of service calls.
function AddInventory2() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Total Inventory');
const data = sh.getRange(1, 1, sh.getLastRow(), 4).getValues(); // A:D
for (let i = 0; i < data.length; i++) {
const valueA = data[i][0];
const valueC = data[i][2];
const valueD = data[i][3];
if (valueA !== "") {
data[i][3] = valueA;
data[i][2] = valueC + valueA;
data[i][0] = "";
data[i][3] = "";
}
}
sh.getRange(1, 1, data.length, 4).setValues(data);
}
Sample Output:
Reference: