CalOohPay API Documentation - v2.1.0
    Preparing search index...

    Class CsvWriter

    Writes on-call payment data to CSV files in a Google Sheets compatible format.

    This class handles the generation of properly formatted CSV files suitable for import into payroll systems, spreadsheet applications (especially Google Sheets), and audit purposes. It includes automatic directory creation, special character escaping, and support for multi-schedule reports.

    The generated CSV includes:

    • Schedule metadata: Name, URL, and timezone
    • Compensation table: User name, total compensation, weekday count, weekend count
    • Proper escaping: Handles commas, quotes, and newlines in data
    • Uses UTF-8 encoding
    • Properly escapes special characters per RFC 4180
    • Formats currency as plain numbers (no symbols)
    • Includes clear headers for easy import

    The class supports appending data from multiple schedules to a single file, automatically separating each schedule's data with blank lines for readability.

    const writer = new CsvWriter('output/august-payments.csv');

    // Write first schedule
    writer.writeScheduleData(
    'Engineering On-Call',
    'https://company.pagerduty.com/schedules/PXXXXXX',
    'Europe/London',
    auditableRecords,
    false // Create new file
    );

    // Append second schedule
    writer.writeScheduleData(
    'SRE On-Call',
    'https://company.pagerduty.com/schedules/PYYYYYY',
    'America/New_York',
    moreRecords,
    true // Append to existing file
    );
    Index

    Constructors

    • Creates a new CsvWriter instance.

      Parameters

      • filePath: string

        Path where the CSV file should be written

      • Optionallogger: Logger

      Returns CsvWriter

      const writer = new CsvWriter('./output/payments.csv');
      

    Methods

    • Deletes the CSV file if it exists.

      Useful for cleanup or ensuring a fresh start before generating new reports. Does nothing if the file doesn't exist (no error thrown).

      Returns void

      // Clean up before generating new report
      writer.deleteIfExists();
      writer.writeScheduleData(...); // Creates fresh file
    • Checks whether the CSV file already exists on the filesystem.

      Returns boolean

      true if file exists, false otherwise

      if (writer.fileExists()) {
      console.log('File already exists - appending data');
      } else {
      console.log('Creating new file');
      }
    • Writes a complete schedule's data to the CSV file.

      Generates a formatted CSV section containing schedule metadata and a table of user compensation data. Can either create a new file or append to an existing one for multi-schedule reports.

      Parameters

      • scheduleName: string

        Human-readable name of the PagerDuty schedule

      • scheduleUrl: string

        Direct URL to the schedule in PagerDuty

      • timezone: string

        IANA timezone identifier used for OOH calculations

      • auditableRecords: Record<string, OnCallCompensation>

        Compensation records for all users in the schedule

      • append: boolean = false

        If true, appends to existing file; if false, creates new file

      Returns void

      If file write operation fails

      Schedule name:,Engineering Primary On-Call
      Schedule URL:,https://company.pagerduty.com/schedules/PXXXXXX
      Using timezone:,Europe/London

      User,Total Compensation (£),Weekdays (Mon-Thu),Weekends (Fri-Sun)
      John Doe,275,1,3
      Jane Smith,400,2,4
      ...
      • When append=false: Overwrites any existing file
      • When append=true: Adds data to end of file with separator line
      • Use append for combining multiple schedules into one report
      const writer = new CsvWriter('august-payments.csv');

      // First schedule - create file
      writer.writeScheduleData(
      'Engineering On-Call',
      'https://company.pagerduty.com/schedules/PXXXXXX',
      'Europe/London',
      engineeringRecords,
      false
      );

      // Second schedule - append
      writer.writeScheduleData(
      'SRE On-Call',
      'https://company.pagerduty.com/schedules/PYYYYYY',
      'America/New_York',
      sreRecords,
      true
      );