如何对 Google 文档中 table 行中的值求和?
How to sum values in rows in a table in Google Docs?
我每行有 table 个不同的价格。我如何对 google 文档中的这些行求和并用“sum=”显示?
单位名称描述价格
<> <> <>
<> <> <>
总和=
我找到了这个 script 并测试了它。
function sumTable() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var tables = body.getTables();
var numRows = tables[0].getNumRows();//specify the table in [ ] that you're working with
var row;
var val;
var total = 0.0;
for(row = 1; row<numRows-1; row++) {
val = tables[0].getCell(row, 2).getText();
if(val) {
total += parseFloat(val);
}
}
tables[0].getCell(numRows -1,2).setText("$"+total.toString());
}
现在,当您在 Apps 脚本项目中单击“运行”时,如果您想 运行 来自 [=18= 的脚本,它将执行并更新 table ] 文档,您可以 运行 这个其他功能,它会在 Google 文档菜单的“Add-ons”下添加一个选项。
function onOpen(e) {
var menu = DocumentApp.getUi().createAddonMenu();
menu.addItem("Sum Table", "sumTable");
menu.addToUi();
}
我每行有 table 个不同的价格。我如何对 google 文档中的这些行求和并用“sum=”显示?
单位名称描述价格 <> <> <> <> <> <>
总和=
我找到了这个 script 并测试了它。
function sumTable() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var tables = body.getTables();
var numRows = tables[0].getNumRows();//specify the table in [ ] that you're working with
var row;
var val;
var total = 0.0;
for(row = 1; row<numRows-1; row++) {
val = tables[0].getCell(row, 2).getText();
if(val) {
total += parseFloat(val);
}
}
tables[0].getCell(numRows -1,2).setText("$"+total.toString());
}
现在,当您在 Apps 脚本项目中单击“运行”时,如果您想 运行 来自 [=18= 的脚本,它将执行并更新 table ] 文档,您可以 运行 这个其他功能,它会在 Google 文档菜单的“Add-ons”下添加一个选项。
function onOpen(e) {
var menu = DocumentApp.getUi().createAddonMenu();
menu.addItem("Sum Table", "sumTable");
menu.addToUi();
}