Google sheets- 使用 VLOOKUP 类型的操作将单元格内容复制并粘贴到单元格的脚本
Google sheets- script to copy and paste cell contents to cell using VLOOKUP type of action
我想创建一个脚本,我可以将其分配给 Google 工作表中的按钮,该脚本会将给定列中的单元格内容复制并粘贴到与给定日期对应的同一列中的单元格(在 A1 中指定)。
在下面的示例中,我想将“hello”从 B2 复制到对应于 4/4/22(A1 中的值)的行,因此我想将“hello”从 B2 复制到B23.
我能够获得可以专门复制到 B23 的代码,但我需要目的地是动态的(根据 A1 中的日期进行更改)——这就是我希望使用某种代码的地方模仿 VLOOKUP。如有任何帮助,我们将不胜感激!
这是示例电子表格:https://docs.google.com/spreadsheets/d/1xG7Tqp2F9EC6VSDDIIzbaXvlkY3toA-ZR3QIWHAvVPA/edit#gid=0
function CopyTo() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('B23').activate();
spreadsheet.getRange('B2').copyTo(spreadsheet.getActiveRange(),
SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
};
可以是公式:
=if(eq($A3,$A),B,"")
将公式放入范围 B3:C39
的单元格中
或者你可以使用数组公式:
=ARRAYFORMULA(if(eq($A3:A,$A),B:C,""))
需要放入单元格B3
并清理B4:C
.
范围内的所有单元格
这是脚本:
function copy_from_b2() {
copy_to_row('b')
}
function copy_from_c2() {
copy_to_row('c')
}
function copy_to_row(column) {
var sheet = SpreadsheetApp.getActiveSheet();
var date = sheet.getRange('a1').getDisplayValue();
var dates = sheet.getRange('a3:a').getDisplayValues().flat().filter(String);
var row = dates.indexOf(date) + 3;
if (row < 3) return;
var value = sheet.getRange(column + 2).getValue();
sheet.getRange(column + row).setValue(value);
}
您可以将函数 copy_from_b2()
分配给蓝色按钮,将 copy_from_c2()
分配给红色按钮。
所以我从@YuriKhristich 那里获取了代码并尝试添加注释来解释它是如何工作的,但是有些部分我不明白。尤里,如果你能澄清或者如果其他人能澄清,那就太好了。
//this function essentially acts as a variable for the copy_to_row(column) function below
function copy_from_b2() {
copy_to_row('b')
}
//this function essentially acts as a variable for the copy_to_row(column) function below
function copy_from_c2() {
copy_to_row('c')
}
function copy_to_row(column) {
var sheet = SpreadsheetApp.getActiveSheet();
var date = sheet.getRange('a1').getDisplayValue();//grabs the date in A1 that will be searched for in column A
var dates = sheet.getRange('a3:a').getDisplayValues().flat().filter(String);//Gets the range of all the dates from A3 to bottom of column. I don't understand what this portion of the code does, however: getDisplayValues().flat().filter(String)
var row = dates.indexOf(date) + 3;//find the row that contains the match for the date (in A1) within the dates range. Three is added to dates.indexOf(date) because the search starts in 4th row.
if (row < 3) return;//This line has me completely confused. I thought return was a keyword to tell Apps Script that you're going to return a value, but I'm not sure why it's used with if(row<3) since I only want to return values in rows>3
var value = sheet.getRange(column + 2).getValue();//selects the value in either B2 or C2 (depending on whether copy_from_b2() or copy_from_c2() is being used )
sheet.getRange(column + row).setValue(value);//copies value to the target column/row
}
我想创建一个脚本,我可以将其分配给 Google 工作表中的按钮,该脚本会将给定列中的单元格内容复制并粘贴到与给定日期对应的同一列中的单元格(在 A1 中指定)。
在下面的示例中,我想将“hello”从 B2 复制到对应于 4/4/22(A1 中的值)的行,因此我想将“hello”从 B2 复制到B23.
我能够获得可以专门复制到 B23 的代码,但我需要目的地是动态的(根据 A1 中的日期进行更改)——这就是我希望使用某种代码的地方模仿 VLOOKUP。如有任何帮助,我们将不胜感激!
这是示例电子表格:https://docs.google.com/spreadsheets/d/1xG7Tqp2F9EC6VSDDIIzbaXvlkY3toA-ZR3QIWHAvVPA/edit#gid=0
function CopyTo() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('B23').activate();
spreadsheet.getRange('B2').copyTo(spreadsheet.getActiveRange(),
SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
};
可以是公式:
=if(eq($A3,$A),B,"")
将公式放入范围 B3:C39
或者你可以使用数组公式:
=ARRAYFORMULA(if(eq($A3:A,$A),B:C,""))
需要放入单元格B3
并清理B4:C
.
这是脚本:
function copy_from_b2() {
copy_to_row('b')
}
function copy_from_c2() {
copy_to_row('c')
}
function copy_to_row(column) {
var sheet = SpreadsheetApp.getActiveSheet();
var date = sheet.getRange('a1').getDisplayValue();
var dates = sheet.getRange('a3:a').getDisplayValues().flat().filter(String);
var row = dates.indexOf(date) + 3;
if (row < 3) return;
var value = sheet.getRange(column + 2).getValue();
sheet.getRange(column + row).setValue(value);
}
您可以将函数 copy_from_b2()
分配给蓝色按钮,将 copy_from_c2()
分配给红色按钮。
所以我从@YuriKhristich 那里获取了代码并尝试添加注释来解释它是如何工作的,但是有些部分我不明白。尤里,如果你能澄清或者如果其他人能澄清,那就太好了。
//this function essentially acts as a variable for the copy_to_row(column) function below
function copy_from_b2() {
copy_to_row('b')
}
//this function essentially acts as a variable for the copy_to_row(column) function below
function copy_from_c2() {
copy_to_row('c')
}
function copy_to_row(column) {
var sheet = SpreadsheetApp.getActiveSheet();
var date = sheet.getRange('a1').getDisplayValue();//grabs the date in A1 that will be searched for in column A
var dates = sheet.getRange('a3:a').getDisplayValues().flat().filter(String);//Gets the range of all the dates from A3 to bottom of column. I don't understand what this portion of the code does, however: getDisplayValues().flat().filter(String)
var row = dates.indexOf(date) + 3;//find the row that contains the match for the date (in A1) within the dates range. Three is added to dates.indexOf(date) because the search starts in 4th row.
if (row < 3) return;//This line has me completely confused. I thought return was a keyword to tell Apps Script that you're going to return a value, but I'm not sure why it's used with if(row<3) since I only want to return values in rows>3
var value = sheet.getRange(column + 2).getValue();//selects the value in either B2 or C2 (depending on whether copy_from_b2() or copy_from_c2() is being used )
sheet.getRange(column + row).setValue(value);//copies value to the target column/row
}