Google脚本:在编辑ALMOST THERE时将行复制到另一个工作表

时间:2018-05-16 10:29:29

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

我花了好几个小时试图让它工作,所以任何帮助都非常感谢我已经得到以下代码从我的主页复制到日志。但是,我尝试更改的代码在第10列中查找工作表名称。我这样只是默认为表单日志,知道需要改变什么才能实现这一点?提前致谢



function onEdit(e) {
  // see Sheet event objects docs
  // https://developers.google.com/apps-script/guides/triggers/events#google_sheets_events
  var ss = e.source;
  var s = ss.getActiveSheet();
  var r = e.range;
  
  // to let you modify where the action and move columns are in the form responses sheet
  var actionCol = 9;
  var nameCol = 10;
  
  var target = SpreadsheetApp.openById('18L5-RGHBx41sMtvC1-7AoVCo6y3imvtELQFjkhPXtoY')
  var targetsheet = target.getSheetByName('logs');
  
  // Get the row and column of the active cell.
  var rowIndex = r.getRowIndex();
  var colIndex = r.getColumnIndex();
  
  // Get the number of columns in the active sheet.
  // -1 to drop our action/status column
  var colNumber = s.getLastColumn()-1;
  
  // if our action/status col is changed to yes do stuff
  if (e.value == "Yes" && colIndex == actionCol) {
    // get our target sheet name - in this example we are using the priority column
   var targetSheet = s.getRange(rowIndex, nameCol).getValue();
    // if the sheet exists do more stuff
    if (ss.getSheetByName(targetSheet)) { 
      // set our target sheet and target range
      var targetSheet = ss.getSheetByName(targetSheet);
      var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
      // get our source range/row
      var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
      // new sheets says: 'Cannot cut from form data. Use copy instead.' 
      sourceRange.copyTo(targetRange);
      // ..but we can still delete the row after
      r.setValue("Logged")
      // or you might want to keep but note move e.g. r.setValue("moved");
    }
  }
}




1 个答案:

答案 0 :(得分:0)

看起来您正试图通过它的名字获得sheet

以下代码将返回名为Logs的工作表。

var targetSheet = SpreadsheetApp.getActive().getSheetByName('Logs');

Google Apps脚本文档:https://ubuntu101.co.za/apache-web-server/fix-apache-2-4-loading-wrong-mpm-worker-module/