[英]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:我需要在此静态列旁边写入数据:
You can do the following:您可以执行以下操作:
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]]);
}
}
}
};
destinationSheet
, to remove all previous content (headers excluded):destinationSheet
,删除所有以前的内容(不包括标题):destinationSheet.getRange("A2:B").clearContent();
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.appendRow
but setValues
.appendRow
而是setValues
。 First, you have to find the index of the first row in which columns A and B are empty.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.