Untitled

 avatar
unknown
plain_text
a year ago
2.4 kB
13
Indexable
function onEdit(e) {
  try {
    const sheetName   = 'יומן חריגים';  // שם הטאב
    const tsCol       = 1;               // A: תאריך ושעה (פתיחה)
    const firstRow    = 2;               // שורת כותרת היא 1
    const statusCol   = 8;               // H: סטטוס
    const closeCol    = 9;               // I: תאריך סגירה
    const triggerCols = [2,3,4,5,6,7,8]; // B..H: עריכה בהם מחתימה פתיחה

    const sh = e.range.getSheet();
    if (!sh || sh.getName() !== sheetName) return;

    const row = e.range.getRow();
    const col = e.range.getColumn();
    if (row < firstRow) return;

    const tsCell    = sh.getRange(row, tsCol);
    const statusCell= sh.getRange(row, statusCol);
    const closeCell = sh.getRange(row, closeCol);

    // ערכים בעמודות B..H (לזיהוי "שורה ריקה")
    const rowValuesBH = sh.getRange(row, 2, 1, 7).getValues()[0];

    // 2.1 חותמת פתיחה: אם נערך אחד מ-B..H ואין תאריך פתיחה → הכנס עכשיו
    if (triggerCols.includes(col)) {
      if (!tsCell.getValue()) {
        tsCell.setValue(new Date()); // ודא שעיצוב העמודה A הוא Date time
      }
    }

    // 2.2 אם כל B..H ריקים → נקה גם את תאריך הפתיחה
    if (rowValuesBH.every(v => v === "")) {
      tsCell.clearContent();
      // ואם הכל ריק – נקה גם תאריך סגירה (למקרה שנשאר)
      closeCell.clearContent();
      return;
    }

    // 2.3 תאריך סגירה – אם סטטוס "טופל": חתום; אחרת נקה
    // מאחסן ערך הסטטוס אחרי העריכה
    const statusVal = statusCell.getDisplayValue().trim();
    const isClosed  = (statusVal === "טופל");

    // אם העריכה הייתה בעמודת הסטטוס או בכל זאת נבדוק בכל שינוי:
    if (isClosed) {
      // חתום סגירה פעם ראשונה בלבד
      if (!closeCell.getValue()) {
        closeCell.setValue(new Date()); // ודא שעיצוב העמודה I הוא Date time
      }
    } else {
      // אם הסטטוס כבר לא "טופל" → נקה תאריך סגירה (כדי שלא יתבלבלו)
      if (closeCell.getValue()) {
        closeCell.clearContent();
      }
    }

  } catch (err) {
    // Logger.log(err);
  }
}
Editor is loading...
Leave a Comment