用于从一张纸复制并粘贴到另一张纸的数组

时间:2019-07-08 07:44:45

标签: google-apps-script google-sheets

Java脚本的新手,所以在寻求帮助时,我正在设计一个包含两张纸的Holiday电子表格

  • 一个包含每个部门团队成员的工作表(表4)
  • 假期表,因此用户可以输入团队假期(Sheet5)。

我在工作表4和工作表5中找到了可以插入行或删除行的脚本,但是现在我需要一个脚本来复制工作表4中新插入的行的内容并将数据粘贴到新的脚本中在工作表5中插入的行与工作表4在同一行。

例如,如果我在工作表4的A10中添加一行并在A10中添加大写字母“ Y”,我希望将B10到I10的数据复制到工作表A10到NJ10的工作表5中。 我试图找到解决方案,但无法使用Copyto命令使它正常工作。

有人对此有解决方案吗?

function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet5 = ss.getSheetByName('Sheet5');
  var sheet4 = ss.getSheetByName("Sheet4");
  var values1 = sheet4.getRange("A:A").getValues();
  var values2 = sheet4.getRange("A:A").getValues();
  var values3 = sheet4.getRange("A:A").getValues();
  // This starts off the remove and Insert Row Code
  var deleteRow = new Array(); // Part of remove and Insert Row Code
  var insertRow = new Array(); // Part of remove and Insert Row Code
  var copyPaste = new Array(); // Copies to Sheet5  
  for (var i = 0; i < values1.length; i++) {
    if (values1[i] == 'D') {
      deleteRow.push(i + 1); // This line was added for debugging purposes.
      var X = sheet4.getRange(i + 1, "2").getValues();
      var response = Browser.msgBox('WARNING!!  You are about to Delete the record for', X, Browser.Buttons.YES_NO);
      Logger.log(response);
      if (response == "yes") {

      } else {
        sheet4.getRange('A:A').clearContent();
        Browser.msgBox('Cancelled', 'The operation has been cancelled', Browser.Buttons.OK);
      }
    }
  }
  for (var i = deleteRow.length - 1; i >= 0; i--) {
    sheet4.deleteRow(deleteRow[i]);
  }
  for (var i = deleteRow.length - 1; i >= 0; i--) {
    sheet5.deleteRow(deleteRow[i]);
  }
  for (var k = 0; k < values2.length; k++) {
    if (values2[k] == 'I') {
      insertRow.push(k + 1); // This line was added for debugging purposes.        
      sheet4.getRange('A:A').clearContent(); //Removes "I" to stop code repeating
    }
  }

  for (var k = insertRow.length - 1; k >= 0; k--) {
    sheet4.insertRowAfter(insertRow[k]);
  }
  for (var k = insertRow.length - 1; k >= 0; k--) {
    sheet5.insertRowAfter(insertRow[k]);
  }
  for (var m = 0; m < values3.length; m++) {
    if (values3[m] == 'Y') {
      copyPaste.push(m + 1); // This line was added for debugging    purposes.        
      sheet4.getRange('A:A').clearContent(); //Removes "Y" to stop code repeating
    }
  }
  for (var m = copyPaste.length - 1; m >= 0; m--) {
    sheet4.copyTo('B2:I13')(copyPaste[m]);
  }
  for (var m = copyPaste.length - 1; m >= 0; m--) {
    sheet4.copyTo('B2:J13')(copyPaste[m]);
  }
}

1 个答案:

答案 0 :(得分:0)

事件对象尽管您选择使用onEdit触发器,但并未使用onEdit()自动生成的任何事件对象。这样可以简化代码并创建确定的IF语句,以验证是否在正确的列,行和表中输入了“ Y”。

代码开发您显然在开发代码时重新定义了目标;包括(最终)不必要的步骤以删除,插入和追加行。这样会使您的代码复杂化,例如为sheet4.getRange("A:A").getValues();

创建的三个相同的变量

推送数组值当您开始将值推送到临时数组时,您遇到了正确的策略来更新“ Sheet5”。在此答案中,我创建了两个嵌套循环以构建必要的值。第一个循环为53周,第二个循环为7天-因此,该阵列每周一次构建53个星期。

此答案中描述的代码只是一种方法。您可能还有其他方式可以实现目标。我还在代码中留下了许多Logger.log()语句;您可能希望在函数的不同阶段使用这些标识值。


function onEdit(e) {

  // so 56930421_04
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ui = SpreadsheetApp.getUi();
  var mastername = "Sheet4";
  var master = ss.getSheetByName(mastername);
  var hols = ss.getSheetByName('Sheet5');

  // get the range for Master and the data also
  var masterLR = master.getLastRow();
  var masterRange = master.getRange(4,2,masterLR-3,8);
  //Logger.log("DEBUG: MASTER range = "+masterRange.getA1Notation());
  var masterData = masterRange.getValues();

  // get the last Column for hols for use in setting the update range
  var holsLC = hols.getLastColumn();
  // Logger.log("DEBUG: the last column on Hols = "+holsLC);

  // display event objects - two mwthods displayed here
  // Method#1
  // Logger.log(JSON.stringify(e))

  // Method#2
  // var debug_e = {authMode:  e.authMode,range:  e.range.getA1Notation(),source:  e.source.getId(),user:  e.user,value:  e.value,oldValue: e. oldValue};
  // Logger.log("DEBUG: AuthMode: "+debug_e.authMode+", Range: "+debug_e.range+", source: "+debug_e.source+", user: "+debug_e.user+", value: "+debug_e.value+", old value: "+debug_e.oldValue);

  // get the column, row and sheet name of the edited cell
  var editColumn = e.range.getColumn();
  var editRow = e.range.getRow();
  var editSheet = e.range.getSheet().getSheetName();
  //Logger.log("DEBUG: editted Column = "+editColumn+", editted Row = "+editRow+", edited Sheet = "+editSheet);

  // set variable
  var firstdatarow = 4;

  // create if statement
  // iF the edited row is between firstdatarow and the last row
  // AND
  // IF the edited column is Column#1  (Column A)
  // AND
  // IF the edited sheet is Master
  if (firstdatarow < editRow && editRow < masterLR && editColumn == 1 && e.value == "Y" && editSheet == mastername) {

    // do something
    // Logger.log("DEBUG: do something");

    // get the employee name
    var empname = masterData[(+editRow-4)][0];
    //Logger.log("DEBUG: The employee name is "+empname);


    // create an alert to confirm
    var result = ui.alert(
      'WARNING!!  You are about to Update the holiday record for '+empname,
      'Are you sure you want to continue?',
      ui.ButtonSet.YES_NO);

    // Process the user's response.
    if (result == ui.Button.YES) {
      // User clicked "Yes".
      ui.alert('Confirmation received.');


      // build the data to be pasted to Sheet5
      var holsData = [];
      for (var i = 1; i<54;i++){ 
        for (var x = 1; x<8;x++){
          holsData.push(masterData[(+editRow-1)][x]);
        }
        //Logger.log("DEBUG: hols = "+holsData+", length = "+holsData.length);
      }
      //Logger.log("DEBUG: Progressive holsData = "+holsData+", length = "+holsData.length);


      // create the range for Sheet5
      var target = hols.getRange(editRow,2,1,(+holsLC-1));
      //Logger.log("DEBUG: the target range = "+target.getA1Notation());

      // update the values to Sheet5
      target.setValues([holsData]);

      //Removes "Y" to stop code repeating
      master.getRange(editRow,editColumn).clearContent();


    } else {

      // User clicked "No" or X in the title bar.
      ui.alert('Permission denied.');

      //Removes "Y" to stop code repeating
      master.getRange(editRow,editColumn).clearContent(); 
    }
  }
  else
  {
    // don't do anything
    Logger.log("don't do anything");
  }  
}