使用Google Apps脚本将特定单元格从一个电子表格复制到同一工作簿中的另一个电子表格

时间:2018-12-07 21:25:10

标签: javascript arrays google-apps-script google-sheets

我有一个Google表单,可将​​客户数据捕获到带有2个标签的Google电子表格工作簿中; sheet1 =流量,sheet2 = ComMan。我试图将在变量“ spot1”中捕获的数据从“流量”选项卡移至“ ComMan”选项卡,但不知道该怎么做。我知道一切工作都符合 sheet.getRange(spot1).copyTo(targetRange); 行。我希望有人能提供帮助。谢谢你的时间。

这是我的代码:

function onEdit() {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var r = sheet.getActiveCell();
var rowIndex = r.getRow();

var sheet1 = "Traffic"; //the active spreadsheet
var sheet2 = "ComMan"; //the ComMan Tab
var status1Col = 19;//status1Col if "No"
var status1Value = "ComMan";//if status1Col = "No", select "ComMan" in the status1Value to send to the ComMan tab

var clientInfo = sheet.getRange(rowIndex, 1, 1, 5);//cols A:E contain client info
var status1Range = sheet.getRange(rowIndex, 11, 1, 5);//cols K:O contain spot 1 info
var comDelValue = sheet.getRange(rowIndex, 60, 1, 1);//col BG contains spot 1 delivery method

var spot1 = [[clientInfo.getValues()],[spot1Info.getValues()],[comDelInfo.getValues]]; //an array to capture all the spot 1 data in one shot

//the statement below will copy the date in spot1 variable and send it to the ComMan tab
if (sheet.getName() == sheet1 && range.getColumn() == status1Col && 
range.getValue() == status1Value){
var targetSheet = ss.getSheetByName(sheet2);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() +1, 1);
sheet.getRange(spot1).copyTo(targetRange);
}
}

1 个答案:

答案 0 :(得分:0)

很显然,您希望能够执行以下操作。因此希望这对您有所帮助。

function copyDisconnectedRangesToAnotherSheet(){
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Sheet22');
  var rowIndex=2
  var vA = sh.getRange(rowIndex, 1, 1, 5).getValues()[0];//cols A:E contain client info
  var vB = sh.getRange(rowIndex, 11, 1, 5).getValues()[0];//cols K:O contain spot 1 info
  var vC = sh.getRange(rowIndex, 60, 1, 1).getValues()[0];//col BG contains spot 1 delivery method
  var vD=vA.concat(vB.concat(vC));
  var rg=sh.getRange(sh.getLastRow()+1,1,1,vD.length);
  rg.setValues([vD]);
  rg.copyTo(ss.getSheetByName('Sheet21').getRange(1,1));//copied to A1
  rg.clearContent();
}