简体   繁体   English

如何使用 Google Sheets Macros 从一张工作表复制值并将它们粘贴到另一张工作表中?

[英]How to copy values from one sheet and paste them into another using Google Sheets Macros?

I'm writing a Google Sheets Macros without having a lot of knowledge about syntax.我正在编写 Google 表格宏,但对语法没有很多了解。

What I want to do is the following:我想要做的是以下内容:

I want to copy the values which are matching in a source matrix into another table.我想将源矩阵中匹配的值复制到另一个表中。 However, I don't know how to write that as a Macros.但是,我不知道如何将其编写为宏。

I've written the following code:我编写了以下代码:

function CalcularCruces() {
  var spreadsheet = SpreadsheetApp.getActive();
  var sourceSheet = spreadsheet.getSheetByName("Cruces Activo-Amenazas");
  var destinationSheet = spreadsheet.getSheetByName("Análisis de Riesgos");

  /** Total number of left column values from source table **/
  const maxAmenazas = 29;

  for(var i = 0; i < maxAmenazas; i++) {
    /** Now I need to get the column and row values which are matching with the checkbox 
        and paste them into another table **/
  }

};

Here is an example of the input table and how the output table should look like after executing the macros.以下是输入表的示例以及执行宏后输出表的外观。

Input Table Sheet输入表Output Table Sheet输出表

Edit:编辑:

I need the data to be written next to this static columns:我需要在此静态列旁边写入数据:

Actual Output实际产量实际产量

Desired Output期望输出期望输出

You can do the following:您可以执行以下操作:

  • Retrieve the data from the source sheet via getDataRange and getValues .通过getDataRangegetValues从源表中检索数据。
  • For each row in this data (excluding the headers row, that has been retrieved and removed from the array with shift ), check which columns have the checkbox marked.对于此数据中的每一行(不包括已使用shift从数组中检索和删除的标题行),检查哪些列标记了复选框。
  • If the corresponding checkbox is marked, write the corresponding values to the destination sheet with setValues .如果相应的复选框被标记,则使用setValues将相应的值写入目标工作表。

It could be something like this:它可能是这样的:

function CalcularCruces() {
  var spreadsheet = SpreadsheetApp.getActive();
  var sourceSheet = spreadsheet.getSheetByName("Cruces Activo-Amenazas");
  var destinationSheet = spreadsheet.getSheetByName("Análisis de Riesgos");
  destinationSheet.getRange("A2:B").clearContent();
  var values = sourceSheet.getDataRange().getValues(); // 2D array with all data from source sheet
  var headers = values.shift(); // Remove and retrieve the headers row
  for (var i = 1; i < values[0].length; i++) { // Iterate through each column
    for (var j = 0; j < values.length; j++) { // Iterate through each row
      var activo = values[j][0]; // Activo corresponding to this row
      if (values[j][i]) { // Check that checkbox is marked
        // Get the row index to write to (first row in which column A and B are empty):
        var firstRow = 2;
        var firstCol = 1;
        var numRows = destinationSheet.getLastRow() - firstRow + 1;
        var numCols = 2;
        var firstEmptyRow = destinationSheet.getRange(firstRow, firstCol, numRows, numCols).getValues().filter(function(row) {
          return row[0] !== "" && row[1] !== "";
        }).length + firstRow;
        // Write data to first row with empty columns A/B:
        destinationSheet.getRange(firstEmptyRow, firstCol, 1, numCols).setValues([[headers[i], activo]]);
      }
    }
  }
};

Notes:笔记:

  • All data is added to the target sheet every time the script is run, and this can lead to duplicate rows.每次运行脚本时,所有数据都会添加到目标工作表中,这可能会导致重复行。 If you want to avoid that, you can use clearContent at the beginning of your script, after declaring destinationSheet , to remove all previous content (headers excluded):如果你想避免这种情况,你可以在脚本的开头使用clearContent ,在声明destinationSheet ,删除所有以前的内容(不包括标题):
destinationSheet.getRange("A2:B").clearContent();
  • In this sample, the number of amenazas is not hard-coded, but it dynamically gets the number of rows in the source sheet with getValues().length .在这个示例中, amenazas的数量不是硬编码的,但它使用getValues().length动态获取源表中的行数。 I'm assuming that's a good outcome for you.我想这对你来说是个好结果。
  • UPDATE : Since you have other columns in your target sheet, you cannot use appendRow but setValues .更新:由于您的目标工作表中有其他列,因此您不能使用appendRow而是setValues First, you have to find the index of the first row in which columns A and B are empty.首先,您必须找到 A 列和 B 列为空的第一行的索引。 This is achieved with filtering the array of values in columns AB and filtering out the elements in which the two values are empty (with filter ).这是通过过滤 AB 列中的值数组并过滤掉两个值为空的元素(使用filter )来实现的。

Reference:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Google 脚本从 1 个 Google 工作表中复制特定值并粘贴到另一个工作表? - How can I copy specific values from 1 Google Sheet and paste to another Sheet using Google Script? Google 工作表脚本将数据集从一张工作表复制到另一张工作表的顶部(仅限值) - Google sheets script copy data sets from one sheet to another (values only) at the top of the sheet 如何将其他谷歌表格中的值添加在一起并将它们显示在一张表格上? - How do i add values from other google sheets together and display them on one sheet? Google电子表格将值从一行复制到另一张工作表 - Google spreadsheet copy values from one row to another sheet 根据条件将单元格值从一个Google工作表复制到另一个 - Copy cell values from one google sheet to another based on condition 将背景颜色从一列复制/粘贴到具有匹配值的列中的另一张工作表 - Copy / paste background color from one column to another sheet in a column with matched values 如何在 Google 表格上将值从一列复制到另一列? - How to copy value from one column to another on Google Sheets? 将值从一个Google表格粘贴到另一个,然后根据ID列删除重复项 - Paste values from one Google Sheet to another and remove duplicates based on ID column 将范围从一个工作表复制到Google工作表中的另一个工作表 - Copy a range from one sheet to another in google sheet 将值从一张纸粘贴到另一张纸并删除重复的纸 - Paste values from one sheet to another and remove duplicates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM