在“编辑副本行脚本”上不起作用

时间:2019-11-29 21:51:18

标签: google-apps-script

在“编辑副本行脚本”不适用于此工作表的情况下, https://docs.google.com/spreadsheets/d/1SJUYN1wayOVzBGWRXNyCDmvoxyanFii6Qy5EBa1pjLo/edit#gid=697654586

当我们使用此表的副本时,此副本行脚本可以正常工作。请帮助,我希望此脚本的帮助也可以在FOOD,PET,KIND,其他客户端,GFS选项卡上运行。

/* 
 **** Move a row onEdit determined by a specific Cell value***
 */

// Names of sheets
var sourceSheet = "PET"
var destinationSheet = "Collate"

/* col: the column to watch,  
 * changeVal: what value you want to change,
 * del: do you want to delete after the change?
 */
var check = {
  "col":17,
  "changeVal": "DELIVERED",
  "del": true ,
  };

/* What you want to paste into the other sheet.
 * start: start column
 * cols: how many columns you want to copy
 */
var pasteRange = {
  "start": 1, 
  "cols": 35
  };
 

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet()
  
  if(sheet.getName() === sourceSheet){
    //Get active cell
    var cell = sheet.getActiveCell();
    var cellCol = cell.getColumn();
    var cellRow = cell.getRow(); 
    
    if(cellCol === check.col){
      if(cell.getValue() === check.changeVal){
        
        //Select the range you want to export
        var exportRange = sheet.getRange(cellRow,pasteRange.start,1,pasteRange.cols);
        
        //Select the past destination
        var pasteDestination = ss.getSheetByName(destinationSheet);
        var pasteEmptyBottomRow = pasteDestination.getLastRow() + 1;
        
        //Copy the row to the new destination
        exportRange.copyTo(pasteDestination.getRange(pasteEmptyBottomRow,1),
                           SpreadsheetApp.CopyPasteType.PASTE_VALUES);
        
        //If delete is true delete after copying
        if(check.del){
          sheet.deleteRow(cellRow);
        };
      };
    };
  };
};

1 个答案:

答案 0 :(得分:0)

尝试一下:

function onEdit(e) {
  e.source.toast('Flag1');
  var sh=e.range.getSheet();
  if(sh.getName()=="Sheet1" && e.range.columnStart==1 && e.value=='DELIVERED') {
    e.source.toast('Flag2');
    var src=sh.getRange(e.range.rowStart,1,1,11);
    var dsh=e.source.getSheetByName('Sheet2');
    var drg=dsh.getRange(dsh.getLastRow() + 1, 1)
    src.copyTo(drg,SpreadsheetApp.CopyPasteType.PASTE_VALUES);
    sh.deleteRow(e.range.rowStart);  
  }
}

您将需要更改工作表名称和一些列号,但现在可以使用。