从 Google 张表格生成发票
Generating invoice from Google Sheets
我有一个 Google 传播sheet,其中我记录了我的自由职业。我设置了每行计算是否付费。 (付款来自单独的 sheet。)
我想做的是生成一张发票,我会在其中 select 客户并获得该客户所有未付款项的列表。
使用数组过滤器函数可以完成这项工作,但我不能将其用作发票,因为我需要下面的总行,并且更喜欢 table 与条目数相匹配的格式。
是否可以将此类信息作为 table 插入到 Google 文档中,或插入表格中,以将数组后面的行向下推?
我认为这将是一个足够简单的概念,但我找不到任何可以完成全部交易的东西。
你可以试试这个脚本。我不确定最终结果是否是您想要的。如果不是,可以轻松修改:
function onEdit(e) {
//If you change the Customer in the Invoice sheet, it runs the code
if (e.range.getA1Notation() == 'A1' && e.source.getSheetName() == 'Invoice'){
var sprsheet = SpreadsheetApp.getActiveSpreadsheet();
var invoice = sprsheet.getSheetByName("Invoice");
var times = sprsheet.getSheetByName("Times");
var in_customer = invoice.getRange("A1").getValue(); //Name you selected in the dropdown menu
var data = times.getRange("A1:H").getValues(); //All the data from the Time sheet
var total = 0;
//Loops through all the data looking for unpaid subtotals from that customer
for (var i = 0; i < data.length; i++){
/*> "i" represents the row, the second number is the column
> The rows start at 0 since it is the first array position.
*/
if (in_customer == data[i][2]) {
if (data[i][7] == 'N'){
total += Number(data[i][5]); //Accumulates each subtotal into total
invoice.appendRow([data[i][0], data[i][1], data[i][3], data[i][5]]);
}
}
}
invoice.appendRow(["Total: ","","", total]);
}
}
这导致(我改变了一些值来测试它):
如您所见,我添加了一些 headers。
参考资料:
我有一个 Google 传播sheet,其中我记录了我的自由职业。我设置了每行计算是否付费。 (付款来自单独的 sheet。)
我想做的是生成一张发票,我会在其中 select 客户并获得该客户所有未付款项的列表。
使用数组过滤器函数可以完成这项工作,但我不能将其用作发票,因为我需要下面的总行,并且更喜欢 table 与条目数相匹配的格式。
是否可以将此类信息作为 table 插入到 Google 文档中,或插入表格中,以将数组后面的行向下推?
我认为这将是一个足够简单的概念,但我找不到任何可以完成全部交易的东西。
你可以试试这个脚本。我不确定最终结果是否是您想要的。如果不是,可以轻松修改:
function onEdit(e) {
//If you change the Customer in the Invoice sheet, it runs the code
if (e.range.getA1Notation() == 'A1' && e.source.getSheetName() == 'Invoice'){
var sprsheet = SpreadsheetApp.getActiveSpreadsheet();
var invoice = sprsheet.getSheetByName("Invoice");
var times = sprsheet.getSheetByName("Times");
var in_customer = invoice.getRange("A1").getValue(); //Name you selected in the dropdown menu
var data = times.getRange("A1:H").getValues(); //All the data from the Time sheet
var total = 0;
//Loops through all the data looking for unpaid subtotals from that customer
for (var i = 0; i < data.length; i++){
/*> "i" represents the row, the second number is the column
> The rows start at 0 since it is the first array position.
*/
if (in_customer == data[i][2]) {
if (data[i][7] == 'N'){
total += Number(data[i][5]); //Accumulates each subtotal into total
invoice.appendRow([data[i][0], data[i][1], data[i][3], data[i][5]]);
}
}
}
invoice.appendRow(["Total: ","","", total]);
}
}
这导致(我改变了一些值来测试它):
如您所见,我添加了一些 headers。
参考资料: