简体   繁体   中英

In google app scripts, how would I make a function that subtracts all the cells in one column from all the respective cells in another column

I have a column 'Amount' and a column 'Shipping'. The amount is the total cost of the item including shipping costs. I am trying to subtract the shipping cost from the amount to see what the actual cost of the item was. I would like the output to be in another column on another spreadsheet called 'sheet_destination'. The original values are from 'sheet_origin1'. Here is the function I have so far. I am getting an error when I run the script at the last line that says "TypeError: Cannot read property '172' of undefined". The '172' refers to the last line of sheet_origin1, however I dont know what I am doing wrong here.

    var amount = sheet_origin1.getRange(3, 7, sheet_origin1.getLastRow(), 1).getValues();
    
    var shipping = sheet_origin1.getRange(3, 10, sheet_origin1.getLastRow(), 1).getValues();
    var grossWixTotal = 0;
    for (var g=0; g<amount.length; g++) {
        for (var h=0; h<shipping.length; h++) {
            grossWixTotal = amount[g][h] - shipping[g][h];
        }
    }
sheet_destination.getRange(sheet_destination.getLastRow(), 4, sheet_origin1.getLastRow(), 1).setValues(grossWixTotal[g][h]);
function cequalsAminusB() {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getActiveSheet();
  const sr=2;
  const rg=sh.getRange(sr,1,sh.getLastRow()-sr+1,3);
  const v=rg.getValues();
  let o=v.map(r=>{r[2]=r[0]-r[1];return r;});
  rg.setValues(o);
}

Initial Data:

A,B,C=A-B
1,0.5,
2,0.6,
3,0.7,
4,0.8,
5,0.9,
6,1,
7,1.1,
8,1.2,
9,1.3,
10,1.4,
11,1.5,
12,1.6,
13,1.7,
14,1.8,
15,1.9,
16,2,
17,2.1,
18,2.2,
19,2.3,

Starting Spreadsheet:

在此处输入图像描述

Ending Spreadsheet:

在此处输入图像描述

Output just column C:

function cequalsAminusB() {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getActiveSheet();
  const sr=2;
  const rg=sh.getRange(sr,1,sh.getLastRow()-sr+1,3);
  const v=rg.getValues();
  let o=v.map(r=>{r[2]=r[0]-r[1];return [r[2]];});
  sh.getRange(sr,3,o.length,o[0].length).setValues(o);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM