[英]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'.我希望 output 位于另一个名为“sheet_destination”的电子表格的另一列中。 The original values are from 'sheet_origin1'.原始值来自“sheet_origin1”。 Here is the function I have so far.这是我目前拥有的 function。 I am getting an error when I run the script at the last line that says "TypeError: Cannot read property '172' of undefined".当我在最后一行运行脚本时出现错误,上面写着“TypeError:无法读取未定义的属性'172'”。 The '172' refers to the last line of sheet_origin1, however I dont know what I am doing wrong here. '172' 指的是 sheet_origin1 的最后一行,但是我不知道我在这里做错了什么。
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: Output 只是列 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.