Action

Flash Cards to CSV

Posted by motopascyyy, Last update over 2 years ago

UPDATES

over 2 years ago

Corrected a scoping bug and a formatting bug

Takes all drafts in the Inbox of a workspace called defined in the Template Tag “flashcard_workspace” and exports the contents to a CSV file “iCloud Drive/Drafts/”. The filename is defined in the Template Tag “flashcard_filename”.

The intention is for the first line in the Draft to be a question for Flash Cards. The second line will be empty. The third line will be the answer.

All questions in all drafts will appear in the first column of the CSV. All answers in all drafts will appear in the second column of the CSV. Once done, the user will be prompted to archive all the Drafts so that they don’t get picked up again. This can be skipped.

Example Draft:

What if…?

Ultron won

Outputs to a CSV file in the format

| What if…? | Ultron won |

Steps

  • defineTemplateTag

    name
    flashcard_workspace
    template
    Flash Cards
  • defineTemplateTag

    name
    flashcard_filename
    template
    flash.csv
  • script

    // Check Workspace Template Tag
    
    let workspaceTag = draft.getTemplateTag("flashcard_workspace");
    if (!workspaceTag) {
    	let message = "The template tag \"flashcard_workspace\" was not defined in this action. Please edit the action and choose the workspace you'd like to use.";
    	alert(message);
    	context.fail(message);
    }
  • script

    // Check Filename Template Tag
    
    let filenameTag = draft.getTemplateTag("flashcard_filename");
    if (! filenameTag) {
    	let message = "The template tag \"flashcard_filename\" was not defined in this action. Please edit the action and choose the workspace you'd like to use.";
    	alert(message);
    	context.fail(message);
    }
  • script

    let workspaceName = draft.getTemplateTag("flashcard_workspace");
    let workspace = Workspace.find(workspaceName);
    let drafts = workspace.query("inbox");
    if (drafts.length == 0) {
        let errorMsg = "Tried finding drafts in the Inbox for the workspace \"Flash Cards\" but nothing found. Exiting action";
        alert(errorMsg);
        context.fail(errorMsg);
    } else {
        let csvText = "";
        for (let d of drafts) {
            let content = d.content.split("\n");
            console.log(d.content);
            let question = content[0];
            let answer = content[2];
            csvText += "\"" + question + "\",\"" + answer + "\"" + "\n";
        }
    
        let fmLocal = FileManager.createCloud(); // Local file in app container
        let filename = draft.getTemplateTag("flashcard_filename");
        let success = fmLocal.writeString("/flash.csv", csvText);
        var prompt = Prompt.create();
        prompt.title = "Archive Flash Cards?";
        prompt.message = `All Flash Cards in the Inbox have been exported to the file "iCloud Drive/Drafts/${filename}". Would you like to archive the drafts?`;
        prompt.isCancellable = false;
        prompt.addButton("Skip");
        prompt.addButton("Archive");
    
        let choice = prompt.show();
    
        if (prompt.buttonPressed == "Archive") {
            for (let d of drafts) {
                d.isArchived = true;
                d.update();
            }
        }
    
    }

Options

  • After Success Default
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.