简体   繁体   English

在谷歌应用程序脚本中,我将如何制作一个 function 从另一列中的所有相应单元格中减去一列中的所有单元格

[英]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.

相关问题 如何记录谷歌应用脚本列中所有单元格的值 - How to Log value of all cells in a column on google app scripts Google 应用脚本,用于在更改触发器上从同一行中第 24 列中的单元格中减去第 22 列中的单元格。 我的脚本减去所有行 - Google app script to subtract a cell in column 22 from a cell in column 24 in the same row on a change trigger. The script I have subtracts all rows 使用jQuery在表格列中的所有单元格上运行函数 - Run function on all cells in table column with jQuery 使用谷歌应用程序脚本对列函数中的所有行求和 - Sum all rows in a column function using google app scripts 如何在handsontable中将按钮添加到列的所有单元格? - How to add a button to all the cells of a column in handsontable? 如果列的所有单元格都为空,则 exjts 隐藏列 - exjts hide column if all cells of the column are empty 量角器-如何从一列中获取所有单元格并在Grid的id为“ 0-0”的网格中对它们求和 - Protractor - How to get all cells from one column and sum them when the Grid has id=“0-0” for rows-Columns 隐藏jQuery或CSS中的所有rowpan列单元格 - Hide all the rowspan column cells in jQuery or CSS 使用jQuery选择第n列中的所有单元格 - Select all cells in nth column with jQuery 将链接添加到html表列中的所有单元格 - Add links to all cells in column of html table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM