简体   繁体   English

如何通过单击 Google 表格中的单个按钮将数据复制到另一张表格?

[英]How to copy data to another sheet with a single button click in Google Sheets?

Sheet1表 1

Sheet2表 2

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.

相关问题 将数据复制到另一张Google表格中 - Copy data to another sheet Google Sheets 如何将一张工作表的值复制到另一张工作表Google工作表脚本 - How to copy the value of one sheet to another sheet Google sheets Script 将数据从一个 Google 工作表工作簿工作表复制到另一个工作簿 - Copy Data from One Google sheets Workbook Sheet to another Workbook 如何制作一张纸的副本并将其保存到Google表格中的文件夹中 - How to make a copy of a single sheet and save it to a folder in google sheets Google 工作表将整个工作表复制到另一个工作表 - Google sheets copy whole sheet to another 用于将范围复制到另一张工作表的 Google 表格脚本 - Google Sheets script to copy a range to another sheet Google 表格 - 将多张表格中的行复制到一个汇总表格中 - Google Sheet - Copy Rows from Multiple Sheets into a Single Summary Sheet Google 工作表脚本将数据集从一张工作表复制到另一张工作表的顶部(仅限值) - Google sheets script copy data sets from one sheet to another (values only) at the top of the sheet 从母版工作表的最后一行复制数据,并将其粘贴到Google工作表中另一工作表的特定单元格 - Copy data from last row of master sheet and paste it in specific cells in another sheet in google sheets Google 表格 - 在 A 列上启用复选框并将该数据发送到另一张表格后,如何复制 11 列数据? - Google Sheets - How can I copy 11 columns of data after enabling a checkbox on column A and sending that data to another sheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM