简体   繁体   中英

A google spreadsheet script that triggers on edit of a column in a sheet and overwrites the value of a user defined cell or not depending on the value

Hope you are all well.

I am trying to write this nice script for my office because my colleagues lack a bit of discipline.

This script should trigger if a column in a sheet is edited, preferably, but it's fine if it trigger on sheet edit or maybe even on spreadsheet edit. This script should look at the value in that column of that sheet and if any of them have the strings "On hold(i)","On hold(ii)" or "On hold(iii)" then the cell on another column (chosen by the user) on the same row should be overwritten with the string "TBC". I tried piecing this from google and below is what I got but since I am here that obviously doesn't work haha. Any help would be greatly appreciated !! :((

function OnEdit() {
    var a=1;
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    while ( a<200 ){
        if ( ss.getSheetByName('Active Jobs').getRange(a,12) == "On hold (i)" ) {
            ss.getSheetByName('Active Jobs').getRange(a,15).setValues("TBC");
            a=a+1;
        }
    }
}

I read somewhere on google that naming the funciton OnEdit would make the function trigger if the spreadsheet was edited but it doesn't seem to work.

function OnEdit() {
   var a=1;
   var ss = SpreadsheetApp.getActiveSpreadsheet();
  var activeSheet = ss.getActiveSheet();
  var activeCell = activeSheet.getActiveCell();

  //Check if the sheet is a JOb sheet and the cell us the status cell
  if (activeSheet.getName().indexOf("Job ID") != -1 && activeCell.getRow() == 2 && activeCell.getColumn() == 15){
    var switchValue = activeCell.getValue();
    switch (switchValue){
      case "On hold (i)":
      case "On hold (ii)": 
      case "On hold (iii)":
      case "To be assigned":
        //Write date to active jobs sheet
        addDateToActive("TBC");
        break;
      case "In progress":
        var newDate = Browser.inputBox("Please enter report out date");
        addDateToActive(newDate);
        break;
      default:
        Browser.msgBox("GOTHERE");
    }
  }
}
function addDateToActive(input){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var activeSheet = ss.getActiveSheet();
  var activeCell = activeSheet.getActiveCell();
  var jobid = activeSheet.getRange(2,1).getValue().toString();
  var activeJobSheet = ss.getSheetByName("Active Jobs");
  var activeJobs = activeJobSheet.getRange(1,1,activeJobSheet.getLastRow(),1).getValues();
  activeJobs = ColumnToArray(activeJobs);
  var jobrow = activeJobs.indexOf(jobid)+1;
  if (jobrow == -1){
    Browser.msgBox("Job Id not preent on Active Jobs sheet");
  }else{
    activeJobSheet.getRange(jobrow,15).setValue(input);
  }
}

function ColumnToArray(columnData){
  //Ensure that the data supplied is a column
  if(columnData[0].length == 1){
    //Convert column to array
    var dataout = [];
    for (var a = 0; a < columnData.length; a++){
      dataout.push(columnData[a][0]);
    }
  }else{
    throw new Error("Data Supplied to ColumnToArray was not a simple Column");
  }
  return dataout;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM