[英]How to copy data to another sheet with a single button click in Google Sheets?
I'm wondering if anyone could help me create a appscript for terminal invoicing and database purposes?我想知道是否有人可以帮我创建一个用于终端发票和数据库目的的 appscript? When I click "SUBMIT" as a button in sheet 1 image, it must reflect line by line like in Sheet 2 image.
当我在表格 1 图像中单击“提交”作为按钮时,它必须像表格 2 图像中那样逐行反映。 (see sheet 1 image and sheet 2 image)
(见表 1 图像和表 2 图像)
-after clicking submit it sheet 1 must auto clear - 点击提交后,表 1 必须自动清除
-when I input another set of data after clearing it needs to keep going down line per line in sheet 2 - 当我在清除后输入另一组数据时,它需要在表 2 中每行继续向下
You can use the following code for that:您可以为此使用以下代码:
function myFunction() {
// declarations
var ss = SpreadsheetApp.getActive();
var sourceSheet = ss.getSheetByName('YOUR_SOURCE_SHEET_NAME');
var database = ss.getSheetByName('YOUR_DATABASE_SHEET_NAME');
// obtain common variables for each item
var dateOfOrder = sourceSheet.getRange('B1').getValue();
var dateOfDelivery = sourceSheet.getRange('B2').getValue();
var agent = sourceSheet.getRange('B8').getValue();
var customer = sourceSheet.getRange('B3').getValue();
// compute variables to initiate reading item rows
var rows = [];
var lastItemRow = sourceSheet.getLastRow();
var firstItemRow = 11;
var nItems = lastItemRow - firstItemRow + 1;
if (nItems < 1) return;
// get row-level information and append to database sheet
var itemNames = sourceSheet.getRange(firstItemRow, 1, nItems, 1).getValues();
var itemQuantities = sourceSheet.getRange(firstItemRow, 2, nItems, 1).getValues();
var itemPrices = sourceSheet.getRange(firstItemRow, 3, nItems, 1).getValues();
for (var i=0; i<nItems; i++) {
var itemName = itemNames[i][0];
var itemQuantity = itemQuantities[i][0];
var itemPrice = itemPrices[i][0];
database.appendRow([dateOfOrder, dateOfDelivery, agent, customer, itemName,
itemQuantity, itemPrice, itemQuantity * itemPrice]);
}
// clear source sheet
sourceSheet.getRange("B1:B8").clear();
sourceSheet.getRange("A11:H").clear();
}
The idea is to first obtain data from your source sheet (using getRange()
along with getValue()
or getValues()
) and afterwards insert it using appendRow()
.这个想法是首先从源表中获取数据(使用
getRange()
以及getValue()
或getValues()
),然后使用appendRow()
插入它。
Finally, you can clear the range using the clear()
method.最后,您可以使用
clear()
方法清除范围。
In order to create a button that calls this script, I suggest you check out this ( Google spreadsheet - Making buttons that add data into cells into another sheet when pressed ) other Stackoverflow answer.为了创建一个调用此脚本的按钮,我建议您查看此( Google 电子表格 - 制作按钮,在按下时将数据添加到另一个工作表中的单元格中)其他 Stackoverflow 答案。 When assigning the script to the image, the name of it should be "myFunction" (without the quotes).
将脚本分配给图像时,其名称应为“myFunction”(不带引号)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.