Beispielszenario für Office-Skripts: Punch Clock-Schaltfläche

Die in diesem Beispiel verwendete Szenarioidee und das Skript wurden vom Office Scripts-Communitymitglied Brian Gonzalez beigesteuert.

In diesem Szenario erstellen Sie ein Arbeitszeitblatt für einen Mitarbeiter, mit dem er seine Start- und Endzeiten mit einer Schaltfläche aufzeichnen kann. Basierend auf dem, was zuvor aufgezeichnet wurde, beginnt das Auswählen der Schaltfläche entweder den Tag (Uhr ein) oder endet den Tag (Uhr aus).

Eine Tabelle mit drei Spalten (

Setupanweisungen

  1. Laden Sie die Beispielarbeitsmappe auf OneDrive herunter.

    Eine Tabelle mit drei Spalten:

  2. Öffnen Sie die Arbeitsmappe in Excel.

  3. Wählen Sie auf der Registerkarte Automatisierendie Option Neues Skript aus, und fügen Sie das folgende Skript in den Editor ein.

    /**
     * This script records either the start or end time of a shift, 
     * depending on what is filled out in the table. 
     * It is intended to be used with a Script Button.
     */
    function main(workbook: ExcelScript.Workbook) {
      // Get the first table in the timesheet.
      const timeSheet = workbook.getWorksheet("MyTimeSheet");
      const timeTable = timeSheet.getTables()[0];
    
      // Get the appropriate table columns.
      const clockInColumn = timeTable.getColumnByName("Clock In");
      const clockOutColumn = timeTable.getColumnByName("Clock Out");
      const durationColumn = timeTable.getColumnByName("Duration");
    
      // Get the last rows for the Clock In and Clock Out columns.
      let clockInLastRow = clockInColumn.getRangeBetweenHeaderAndTotal().getLastRow();
      let clockOutLastRow = clockOutColumn.getRangeBetweenHeaderAndTotal().getLastRow();
    
      // Get the current date to use as the start or end time.
      let date: Date = new Date();
    
      // Add the current time to a column based on the state of the table.
      if (clockInLastRow.getValue() as string === "") {
        // If the Clock In column has an empty value in the table, add a start time.
        clockInLastRow.setValue(date.toLocaleString());
      } else if (clockOutLastRow.getValue() as string === "") {
        // If the Clock Out column has an empty value in the table, 
        // add an end time and calculate the shift duration.
        clockOutLastRow.setValue(date.toLocaleString());
        const clockInTime = new Date(clockInLastRow.getValue() as string);
        const clockOutTime  = new Date(clockOutLastRow.getValue() as string);
        const clockDuration = Math.abs((clockOutTime.getTime() - clockInTime.getTime()));
    
        let durationString = getDurationMessage(clockDuration);
        durationColumn.getRangeBetweenHeaderAndTotal().getLastRow().setValue(durationString);
      } else {
        // If both columns are full, add a new row, then add a start time.
        timeTable.addRow()
        clockInLastRow.getOffsetRange(1, 0).setValue(date.toLocaleString());
      }
    }
    
    /**
     * A function to write a time duration as a string.
     */
    function getDurationMessage(delta: number) {
      // Adapted from here:
      // https://stackoverflow.com/questions/13903897/javascript-return-number-of-days-hours-minutes-seconds-between-two-dates
    
      delta = delta / 1000;
      let durationString = "";
    
      let days = Math.floor(delta / 86400);
      delta -= days * 86400;
    
      let hours = Math.floor(delta / 3600) % 24;
      delta -= hours * 3600;
    
      let minutes = Math.floor(delta / 60) % 60;
    
      if (days >= 1) {
        durationString += days;
        durationString += (days > 1 ? " days" : " day");
    
        if (hours >= 1 && minutes >= 1) {
          durationString += ", ";
        }
        else if (hours >= 1 || minutes > 1) {
          durationString += " and ";
        }
      }
    
      if (hours >= 1) {
        durationString += hours;
        durationString += (hours > 1 ? " hours" : " hour");
        if (minutes >= 1) {
          durationString += " and ";
        }
      }
    
      if (minutes >= 1) {
        durationString += minutes;
        durationString += (minutes > 1 ? " minutes" : " minute");
      }
    
      return durationString;
    }
    
  4. Benennen Sie das Skript in "Punch Clock" um.

  5. Speichern Sie das Skript.

  6. Wählen Sie in der Arbeitsmappe Zelle E2 aus.

  7. Schaltfläche "Skript hinzufügen". Wechseln Sie zum Menü Weitere Optionen (...) auf der Seite Skriptdetails , und wählen Sie In Arbeitsmappe hinzufügen aus.

  8. Speichern Sie die Arbeitsmappe.

Ausführen des Skripts

Wählen Sie die Schaltfläche Punch Clock aus, um das Skript auszuführen. Es protokolliert entweder die aktuelle Zeit unter "Clock In" oder "Clock Out", je nachdem, was zuvor eingegeben wurde.

Die Tabelle und die Schaltfläche

Hinweis

Die Dauer wird nur aufgezeichnet, wenn sie länger als eine Minute ist. Bearbeiten Sie die Uhrzeit des "Eintaktens" manuell, um größere Daueren zu testen.