将数据从工作表移动到工作表google工作表

时间:2015-07-27 23:50:45

标签: google-apps-script google-sheets

我的工作表从表单回复中获取数据。我有多个标签用于不同的请求类型。目前,当提交表单时,它会进入主选项卡,我手动将列“状态”第7列更改为“待定分配”,并使用以下脚本将其发送到相应的选项卡。有没有办法实现自动化?或者当请求进入时根据第4列(请求类型)中的内容移动到选项卡?

  function myEdit(e){

  var ss = e.source;
  var s = ss.getActiveSheet();
  var r = e.range;

  var actionCol = 7;
  var nameCol = 4;

  var rowIndex = r.getRowIndex();
  var colIndex = r.getColumnIndex();

  var colNumber = s.getLastColumn();

  if ((e.value == "Pending Assignment" && colIndex == actionCol)) {
   var targetSheet = s.getRange(rowIndex, nameCol).getValue();
     if (ss.getSheetByName(targetSheet)) { 
      var targetSheet = ss.getSheetByName(targetSheet);
      var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);

  var sourceRange = s.getRange(rowIndex, 1, 1, colNumber); 
  sourceRange.copyTo(targetRange);
  s.deleteRow(rowIndex);
}  
}
}

1 个答案:

答案 0 :(得分:0)

而不是onEdit,您可以使用onFormResponse触发器。

function onFormResponse() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var formsheet = ss.getSheetByName("Form Responses 1");
  var lastrow = formsheet.getLastRow();

  var actionCol = 7;
  var nameCol = 4;
  var colNumber = formsheet.getLastColumn();

  formsheet.getRange(lastrow, actionCol).setValue("Pending Assignment");

  var targetSheet = s.getRange(lastrow, nameCol).getValue();

  if (ss.getSheetByName(targetSheet)) { 
    var targetSheet = ss.getSheetByName(targetSheet);
    var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);

    var sourceRange = formsheet.getRange(lastrow, 1, 1, colNumber); 
    sourceRange.copyTo(targetRange);
    formsheet.deleteRow(lastrow);
  }
}