简体   繁体   English

如何使用 Google 脚本从 1 个 Google 工作表中复制特定值并粘贴到另一个工作表?

[英]How can I copy specific values from 1 Google Sheet and paste to another Sheet using Google Script?

So I already have a script that is copy/pasting the entire row from one sheet to another.所以我已经有一个脚本,可以将整行从一张纸复制/粘贴到另一张纸上。 But I am interested in only copy/pasting specific cellls.但我只对复制/粘贴特定单元格感兴趣。 For example, if column C = Approved, copy value in Column G in source sheet and paste to new row in column H of destination sheet.例如,如果列 C = 已批准,则复制源工作表 G 列中的值并粘贴到目标工作表 H 列的新行。 Below are example screenshots and my current code:以下是示例屏幕截图和我当前的代码:

Source sheet (only copy email in columnG) Source Sheet源表(仅在 G 列复制 email)源表

Destination sheet (paste email in columnH) Destination sheet目标表(在 H 列粘贴 email)目标表

function copyrange() {

 var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Test 2'); //source sheet
  var testrange = sheet.getRange('C:C'); //range to check
  var testvalue = (testrange.getValues());
  var csh = ss.getSheetByName('Test'); //destination sheet
  var data = [];
  var j =[];

  //Condition check in H:H; If true copy the same row to data array
for (i=0; i<testvalue.length;i++) {
  if ( testvalue[i] == 'Approved') {
  data.push.apply(data,sheet.getRange(i+1,1,1,25).getValues());
  //Copy matched ROW numbers to j
  j.push(i);
 }
 }
//Copy data array to destination sheet

 csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);

//Delete matched rows in the source sheet
  for (i=0;i<j.length;i++){
  var k = j[i]+1;
  sheet.deleteRow(k);

//Alter j to account for deleted rows
  if (!(i == j.length-1)) {
  j[i+1] = j[i+1]-i-1;
}
}
}

Is anyone able to help me with this?: I would greatly appreciate it :) Thank you!有人能帮我解决这个问题吗?:我将不胜感激:) 谢谢!

I made some corrections.我做了一些更正。 Could not test it without your sample sheet没有您的样品表无法测试


function copyrange() {

 var ss = SpreadsheetApp.getActiveSpreadsheet();
  var srcsht = ss.getSheetByName('Test 2'); //source sheet
  var srcrange = sheet.getRange('C:G'); //range to check - edited
  var srcvals= testrange.getValues(); // edited
  var dessht = ss.getSheetByName('Test'); //destination sheet
  var emails =  [];
  var delrows =[];

  //Condition check in H:H; If true copy the same row to data array
for (i=0; i < srcvals.length;i++) {
  if ( srcvals[i][0] == 'Approved') {
   emails.push(srcvals[i][4]) ; //edited - col G of same row
  //Copy matched ROW numbers to j
  delrows.push(i);
 }
 }
//Copy data array to destination sheet

 dessht.getRange(dessht.getLastRow()+1,1,emails.length,emails[0].length).setValues(emails);

//Delete matched rows in the source sheet
  for (i= delrows.length; i > 0; i--){
   srcsht.deleteRow(delrows[i]+1);
}
}

暂无
暂无

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

相关问题 如何使用 Google Sheets Macros 从一张工作表复制值并将它们粘贴到另一张工作表中? - How to copy values from one sheet and paste them into another using Google Sheets Macros? Google Script,如何使用正则表达式将一行从 Sheet1 复制到 Sheet2 而不是一个单元格? - Google Script, how can I copy a row from Sheet1 to Sheet2 instead of just one cell by using regex? 使用Google表格脚本移动过滤后的值(复制和粘贴值),而忽略列标题/标题 - Moving filtered values (copy and paste values) using google sheet script while ignoring the column heading / title Google 工作表脚本将数据集从一张工作表复制到另一张工作表的顶部(仅限值) - Google sheets script copy data sets from one sheet to another (values only) at the top of the sheet Google脚本:如何将范围复制并粘贴到不为空的行中的新表中 - Google Script: How to copy and paste range to a new sheet on a row that is not empty Google Apps脚本-从另一个工作表导入工作表值 - Google apps script - Importing sheet values from another sheet 如果值存在于工作表 1 和工作表 2 中,则复制行并将其粘贴到工作表 3。Google 表格。 Google Apps 脚本 - Copy row if value exists in both sheet 1 and sheet 2 and paste it to sheet 3. Google Sheets. Google Apps Script 将工作表复制到另一个电子表格[Google Apps脚本] - Copy sheet to another spreadsheet [Google Apps Script] 将数据从一张纸复制到另一张 - Google Script - Copy Data from one sheet to another - Google Script 用于匹配和复制基于另一个相邻单元格的 Google 表格脚本 - Google sheet script to match and copy based from another adjacent cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM