我希望创建一个脚本,它可以分配给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);
};
发布于 2022-04-13 19:12:42
下面是脚本:
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()
分配给红色按钮。
发布于 2022-04-04 21:12:19
它可以是这样的公式:
=if(eq($A3,$A$1),B$2,"")
将公式放入范围B3:C39
的单元格中
也可以使用数组公式:
=ARRAYFORMULA(if(eq($A3:A,$A$1),B$2:C$2,""))
它需要放入单元格B3
并清洗范围内的所有单元格B4:C
。
发布于 2022-04-14 23:00:14
因此,我从@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
}
https://stackoverflow.com/questions/71742742
复制相似问题