仅触发一个Google Apps脚本事件

时间:2012-10-31 13:56:48

标签: google-apps-script

下面的Google Apps脚本只会触发一些事件。例如,如果我将第16列更改为X,则会正确更新第17列。但是,如果我然后删除第16列中的X,它会删除第17列中的日期,但不会重置fontcolor(dataRangeAll.setFontColor(false)中的colorRow(r))。如果它本身就是fontcolor重置的话。

是在两个事件之后运行两个事件,还是以某种方式下面的代码错误?

示例电子表格can be found here

function setDate(r){
  var sheet = SpreadsheetApp.getActiveSheet();

  var dataRange = sheet.getRange(r, 1, 1, 17);

  var data = dataRange.getValues();
  var row = data[0];


  var date = new Date();
  date.setHours(0);
  date.setMinutes(0);
  date.setSeconds(0);
  //Logger.log(date);

  var Sdate = Utilities.formatDate(date,'GMT+0100','yyyy-MM-dd')


  if(row[2] != ""){ 
    sheet.getRange(r,12).setValue(Sdate);
  } else if(row[2] == ""){ 
    sheet.getRange(r,12).setValue("");
  }

  SpreadsheetApp.flush(); 

}

function setDate2(r){
  var sheet = SpreadsheetApp.getActiveSheet();

  var dataRange = sheet.getRange(r, 1, 1, 17);

  var data = dataRange.getValues();
  var row = data[0];


  var date = new Date();
  date.setHours(0);
  date.setMinutes(0);
  date.setSeconds(0);
  //Logger.log(date);

  var Sdate = Utilities.formatDate(date,'GMT+0100','yyyy-MM-dd')


  if(row[15] != ""){ 
    sheet.getRange(r,17).setValue(Sdate);
  } else if(row[15] == ""){ 
    sheet.getRange(r,17).setValue("");
  }

  SpreadsheetApp.flush(); 

}


function colorRow(r){
  var sheet = SpreadsheetApp.getActiveSheet();
  var dataRange = sheet.getRange(r, 12, 1, 6);
  var dataRangeAll = sheet.getRange(r, 1, 1, 17);

  var data = dataRange.getValues();
  var row = data[0];

  var date = new Date();
  date.setHours(0);
  date.setMinutes(0);
  date.setSeconds(0);
  //Logger.log(date);
  var sheetDate = new Date(row[0]);

  var Sdate = Utilities.formatDate(date,'GMT+0100','yyyy-MM-dd')
  var SsheetDate = Utilities.formatDate(sheetDate,'GMT+0100', 'yyyy-MM-dd')

  dataRangeAll.setFontLine(false);

  //Om faktura har skickats
  if(row[4] != ""){ 
    dataRangeAll.setFontLine("line-through");
    dataRange.setBackgroundRGB(255, 255, 255);
    dataRangeAll.setFontColor('#cccccc');

  //Om faktura inte har skickats och fakturadatum har uppnåtts  
  }else if(row[0] != "" && Sdate >= SsheetDate && row[4] == ""){ 
      dataRange.setBackgroundRGB(255, 170, 86);
      dataRange.setFontColor('#000000');
      dataRangeAll.setFontColor(false);

  //Om faktura inte har skickats, men fakturadatum är tomt  
  }else if(row[0] == ""){ 
    dataRange.setBackgroundRGB(255, 255, 255);
    dataRange.setFontColor('#cccccc');
    dataRangeAll.setFontColor(false);

  //Om faktura inte har skickats, men fakturadatum inte har uppnåtts
  }else{ 
    dataRange.setBackgroundRGB(255, 255, 255);
    dataRange.setFontColor('#777777');
    dataRangeAll.setFontColor(false);
  }




  SpreadsheetApp.flush(); 
}


function onEdit(event)
{
  var r = event.source.getActiveRange().getRowIndex();
  if (r >= 3) {

    var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

    //När en produktion markeras som klar eller inte
    var watchColumn = 3;   //A=1, B=2, C=3...  
    var activeColumn = ss.getActiveCell().getColumn();

    if (activeColumn == watchColumn){  //only run if change in watch column 
      setDate(r);
    }

    //När en faktura markeras som skickad eller inte
    var watchColumn2 = 16;   //A=1, B=2, C=3... 
    var activeColumn2 = ss.getActiveCell().getColumn();

    if (activeColumn2 == watchColumn2){  //only run if change in watch column 
      setDate2(r);
    }

    colorRow(r);
  }
}

0 个答案:

没有答案