简体   繁体   中英

Run a script based on sheet names for different sheets depending on another script

yesterday I posted my first issue ( Archive rows from different sheets and remove them to initial position (to initial sheet) when I want to unarchive (google sheets) ) that has been fixed (thanks again if you are reading me) .

I completed my script by the following code (starting at When a client is mentioned in Col3, fill automatically Col23 or W by the PM name ).

function onEdit(event) {
  // assumes source data in sheet named Elisa-Miriam-Victor
  // target sheet of move to named Archive
  // getColumn with check-boxes is currently set to column 2 or B
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();

  // Archive a project with checked box from Archive Sheet
 if((s.getName() == "Elisa"||s.getName() == "Miriam" )&& r.getColumn() == 2 && r.getValue() == true) { // copy past every time a new PM-sheet is added
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Archive");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
  } 
  // Unarchive a project from Archive sheet to respective owner
  else if(s.getName() == "Archive" && r.getColumn() == 2 && r.getValue() == false) {
    var row = r.getRow();
    var nameColumn = 23;
    var name = s.getRange(row, nameColumn).getValue();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName(name);
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);

    // When a client is mentioned in Col3, fill automatically Col23 or W by the PM name    
    {
  var s = SpreadsheetApp.getActiveSheet();
  var data = s.getRange("C9:C").getValues();
  var data_len = data.length;
  for(var i=8; i<data_len; i++) {
  if(data[i][0].length == 0) {
    s.getRange(i+1,23).setValue("");
  } else {
    s.getRange(i+1,23).setValue("Miriam");
  }
  }
}
}
}

So from now, if, on Miriam's sheet, there is a new client mentioned in Col3, Col 23 is automatically filled by her name. But I need to standardize this part of the script because every new employee has his own sheet (his name) and for every employee's sheet, anytime Col3 is filled, I need to automatically fill Col23 under the name of the sheet.

Does anyone have an idea of how to write the script for Elisa and Miriam for example?

Thank you for your help!

Objective:

Write the Sheet's name if there is a new client added in Col3

Procedure:

You can check the last row of Col23 to see if it's empty and copy the name of the Sheet. The change would only happen if someone manually edits the Col3.

Code:

This is an example. You would have to integrate it into your current code.

function onEdit(event){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();

  var lastRow = s.getLastRow();
  var col23 = s.getRange(lastRow, 23).getValue();

  if (r.getRow() == lastRow && r.getColumn() == 3 && col23 == ""){

    s.getRange(lastRow, 23).setValue(s.getSheetName());
 }
}

References:

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