简体   繁体   English

需要将行从sheet1复制到sheet2,并将单元格值粘贴到sheet2中具有匹配列值的行上

[英]Need to copy row from sheet1 to sheet2 and paste the cell values on a row in sheet2 with a matching column value

I'm working on a Google Sheets that has two sheets in it that I'm using to process data from a submitted Google Forms. 我正在使用其中包含两个工作表的Google表格,用于处理来自已提交的Google表单的数据。 sheet1 is where the new form responses are entered and sheet2 is an "archive" of sorts, so sheet1 only has the most recent entries that I have not looked at yet. sheet1是输入新表单响应的位置,而sheet2是各种“存档”的位置,因此, sheet1仅具有我尚未查看的最新条目。

sheet1 column 8 has a 5-digit number, column 9 is either blank or will say "Rcvd", the rest of the columns hold other data that just needs to be copied, but do not hold unique values. sheet1 8列具有5位数字,第9列为空白或显示为“ Rcvd”,其余各列包含仅需要复制的其他数据,但不包含唯一值。 sheet1 and sheet2 have identical column headers and organization, but in sheet2 , only column 8 is filled in. sheet1sheet2具有相同的列标题和组织,但是在sheet2 ,仅填充了列8。

What I'd like it to do is copy all the values in a given row from sheet1 to sheet2 when the value in column 9 of that row is changed to "Rcvd" (on edit). 我想做的是,当该行的第9列中的值更改为“ Rcvd”(在编辑时)时,将给定行中的所有值从sheet1复制到sheet2 The row's values then need to be inserted into sheet2 in the same columns, on the row with the matching value in column 8. The given row then needs to be deleted from sheet1 . 然后,需要将该行的值插入到sheet2的同一列中,在该行中具有与第8列匹配的值。然后需要从sheet1删除给定的行。

Thus far, I have been able to get the row to move from sheet1 to sheet2 and delete itself in sheet1 , but not paste itself in a row with a matching column value in sheet2 , only at the last row of sheet2 . 到目前为止,我已经能够得到来自移动的行sheet1sheet2 ,并删除自身sheet1 ,而不是本身的匹配列值粘贴在一排sheet2 ,只在最后一排sheet2

I've been using this script: 我一直在使用此脚本:

function onEdit(event) {
// assumes source data in sheet named "sheet1"
// target sheet of move to named "sheet2"
// column with rcvd is col 9 or I

var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();

if(s.getName() == "sheet1" && r.getColumn() == 9 && r.getValue() == "Rcvd") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("sheet2");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}

To me, it looks like the .getRange(targetSheet.getLastRow() +1, 1) is what needs to be changed to get the correct row number in sheet2 based on the column 8 value, but I'm not having much success getting it to work. 在我看来, .getRange(targetSheet.getLastRow() +1, 1)是需要更改的内容,以便基于列8的值在sheet2中获取正确的行号,但是我获得的成功并不多它起作用。

This was quite a doozy, but I have the functionality you want: 这确实有点麻烦,但是我具有您想要的功能:

function onEdit(event) {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
var columnSearchNum = 3;
var columnSearchChar = "C";
var rcvdColumn = 2;
var sourceSheetName = "Sheet1";
var targetSheetName = "Sheet2";

if(s.getName() == sourceSheetName && r.getColumn() == rcvdColumn && r.getValue() == "Rcvd") {
  var sourceRow = r.getRow();
  var targetSheet = ss.getSheetByName(targetSheetName);
  var targetSheetNumRows = targetSheet.getLastRow();
  //console.log(targetSheetNumRows);
  var sourceSheetNumColumns = s.getLastColumn();
  var targetRange = targetSheet.getActiveRange()
  var targetValue = +s.getRange(columnSeachChar+sourceRow).getValue()
  //console.log(targetValue);
  var targetSheetRange = targetSheet.getRange(1,columnSearchNum,targetSheetNumRows,1);
  //console.log(targetSheetRange.getNumRows() +" " +targetSheetRange.getNumColumns() + " " + targetSheetRange.getValues());
  var targetRow = findIndex(targetSheetRange.getValues(), targetValue);
  //console.log(targetRow);
  var target = targetSheet.getRange(targetRow, 1);
  s.getRange(sourceRow, 1, 1, sourceSheetNumColumns).moveTo(target);
  s.deleteRow(sourceRow);
}
}

function findIndex(array, search){
  //console.log(array);
  if(search == "") return false;
  for (var i=0; i<array.length; i++){
    //console.log("comparing " + +array[i] + " to "+ +search);
    if (+array[i] == +search){ 
      return i+1;
    }
  }
  return -1;
} 

You'll just need to change sheet names and columns to match your specifics 您只需要更改工作表名称和列以匹配您的详细信息

暂无
暂无

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

相关问题 如何在 sheet1 的 I 列中找到非空白值,然后复制整行并粘贴到 sheet2 - How to find the non-blank value in column I in sheet1 then copy entire row and paste to sheet2 将Sheet1中的数据复制粘贴到Sheet2,然后删除输入的数据,不删除Sheet1中的function - Copy and paste the data from Sheet1 to Sheet2 and then delete the entered data without deleting the function in Sheet1 Google Script,如何使用正则表达式将一行从 Sheet1 复制到 Sheet2 而不是一个单元格? - Google Script, how can I copy a row from Sheet1 to Sheet2 instead of just one cell by using regex? 用于从 (sheet1) 导入行并增加目标 (sheet2) 上的行间隔的 Google Sheet Script - Google Sheet Script to import rows from (sheet1) and increase row intervals on target (sheet2) Google工作表代码-单击按钮,编辑,将工作表1中的行添加到工作表2中 - Google sheet code - Edit, Add row from sheet1 to sheet2 upon clicking a button 如果单元格上的日期超过 30 天,如何从 Sheet1 剪切线并粘贴到 Sheet2? - How to Cut line from Sheet1 and Paste to Sheet2 if the date on a cell is 30+ days old? 我需要将 Google Sheet 1 中的数据复制到 Google Sheet2 中的一行 - I need to copy Data from Google Sheet 1 to a Row in Google Sheet2 如何获取单元格值(来自 Google 表单的 Sheet1,来自公式的 Sheet2)到谷歌文档模板 - How to get the cell values (Sheet1 from Google Form, Sheet2 from Formula) to google doc template 无法以特定格式将 sheet1 数据复制到 sheet2 - Not able to copy sheet1 data to sheet2 in specific format 如果在sheet2中不存在将单元格从sheet1复制到sheet2 - Copying cells from sheet1 to sheet2 if it does not exist in sheet2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM