This function allows users to generate a report based on task data from their notes. The report can be filtered based on tags, formatted in different file formats (Markdown Table, CSV, TXT, JSON), and downloaded. It involves multiple steps, including collecting user inputs, filtering and sanitizing the data, and then generating the file in the selected format.
Tags (OR) [tagNamesOr]
Purpose: Allows users to filter tasks by tags using an OR logic, where each tag is searched separately.
Type: List of tags (comma-separated string).
Limit: Max 10 tags.
Effect on Output: Filters tasks where the tag matches any one of the tags in this list.
Tags (AND) [tagNamesAnd]
Purpose: Allows users to filter tasks by tags using an AND logic, where all provided tags must be present on the task.
Type: Single tag or list of tags.
Limit: Max 10 tags (typically a single tag group).
Effect on Output: Filters tasks that contain all the specified tags simultaneously.
Download Format [dwFormat]
Purpose: Allows users to select the file format for the report.
Type: One of the following radio options:
Markdown Table (download_md)
CSV (download_csv)
TXT (download_txt)
JSON (download_json)
Effect on Output: Determines how the filtered data will be formatted before download.
Prompting User Input
The user is prompted to select the tags for filtering tasks and to choose the desired format for the downloadable report:
tagNamesOr: Used for tags to be filtered by OR logic.
tagNamesAnd: Used for tags to be filtered by AND logic.
dwFormat: Specifies the format (Markdown, CSV, TXT, JSON) for the downloadable report.
Tag-based Filtering
The function handles the filtering of notes based on the user-specified tags:
OR logic: For each tag in the tagNamesOr input, the function queries tasks that match any of the tags.
AND logic: A single query is executed for tasks containing all the tags in tagNamesAnd.
Sanitization
Task content and note names are sanitized to remove unwanted characters, markdown, and HTML tags before inclusion in the report.
Task content is cleaned up to ensure only text is included.
Note name is sanitized to remove any special characters, keeping only letters, numbers, and spaces.
Sorting
The filtered list of tasks is sorted by the task's name (in ascending alphabetical order) to ensure consistency and readability in the final output.
Formatting Task Details
The following task details are extracted and formatted:
Task start, end, completed, and dismissed times (formatted as date and time).
Task repeat information, if available, formatted to show the frequency and start date/time.
Task attributes like importance, urgency, and score are included as raw data.
Results Collection
After filtering and sanitizing tasks, the details for each task are stored in an array allTasks, which contains an object for each task with its formatted information.
File Format Handling
Based on the user-selected format (dwFormat), the data is converted into the chosen format:
Markdown: Tasks are presented in a table format with task details displayed as rows. Headers and a divider are automatically generated for the table.
CSV: The task details are converted to CSV format, with fields enclosed in quotes to ensure proper escaping of special characters.
TXT: The task details are output in a readable format where each task is represented as a JSON object with its details, formatted for easy reading.
JSON: The tasks are output in JSON format, containing all the structured data as key-value pairs.
Downloading the Report
Once the data is formatted, the file is created as a Blob (binary large object) and downloaded using an HTML link element. The content type and file extension depend on the chosen format.
formatTaskRepeat(repeatInfo)
Purpose: This method formats the repeat information of a task, extracting the frequency and start date/time from the provided string and returning a readable format.
Input: A string containing repeat information, typically formatted with DTSTART and RRULE properties.
Output: A formatted string displaying the repeat frequency and the start time.
formatTimestamp(timestamp)
Purpose: This method formats Unix timestamps into a readable date and time string.
Input: A Unix timestamp (in seconds).
Output: A formatted string of the form MM/DD/YYYY at HH:MM:SS.
toCSV(data)
Purpose: Converts an array of objects (results) into CSV format.
Input: An array of task objects.
Output: A CSV string representing the data, with proper escaping of double quotes.
downloadFile(filename, content, contentType)
Purpose: Downloads a file by creating an HTML link element and triggering a download action.
Input: The file's name, content, and MIME type (e.g., CSV, Markdown).
Output: A file download is triggered automatically in the browser.
No Tags or Download Format: If the user does not provide tags or a download format, the function will show an alert prompting them to make a selection.
Empty or Invalid Data: If no notes or tasks match the selected criteria, the generated report will be empty.
Unsupported Format: If an unknown download format is selected, an error message is logged.
User Selection: The user selects tags and a download format.
Filtering: Notes and tasks are filtered based on the selected tags.
Sanitization & Sorting: Task content and note names are sanitized, and tasks are sorted.
File Creation: Based on the selected format, the tasks are formatted and converted into the appropriate file type.
Download: The file is automatically downloaded to the user's device.
This plugin provides a flexible and dynamic way for users to download filtered task reports in various formats. It ensures that the user can tailor their report based on tags, choose the desired format, and get the data in a clean, readable, and easily downloadable file.