首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Google使用VLOOKUP类型的操作将单元格内容复制和粘贴到单元格的脚本

Google使用VLOOKUP类型的操作将单元格内容复制和粘贴到单元格的脚本
EN

Stack Overflow用户
提问于 2022-04-04 19:25:36
回答 3查看 245关注 0票数 1

我希望创建一个脚本,它可以分配给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

代码语言:javascript
运行
复制
    function CopyTo() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('B23').activate();
spreadsheet.getRange('B2').copyTo(spreadsheet.getActiveRange(), 
SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
};

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-04-13 19:12:42

下面是脚本:

代码语言:javascript
运行
复制
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()分配给红色按钮。

票数 1
EN

Stack Overflow用户

发布于 2022-04-04 21:12:19

它可以是这样的公式:

代码语言:javascript
运行
复制
=if(eq($A3,$A$1),B$2,"")

将公式放入范围B3:C39的单元格中

也可以使用数组公式:

代码语言:javascript
运行
复制
=ARRAYFORMULA(if(eq($A3:A,$A$1),B$2:C$2,""))

它需要放入单元格B3并清洗范围内的所有单元格B4:C

票数 3
EN

Stack Overflow用户

发布于 2022-04-14 23:00:14

因此,我从@YuriKhristich获取了代码,并试图添加一些注释来解释它是如何工作的,但是有一些部分我不明白。尤里,如果你能澄清或者其他人可以的话,那就太好了。

代码语言:javascript
运行
复制
//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
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71742742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档