Generate Text File
This item converts any text into a file and adds it to a File-type data item.
Configs: Common
  • Step Name
  • Note
Configs
  • C1: File type data item to save text file *
  • C2: Delete other files when saving
  • C3: Saving file name *#{EL}
  • C4: Contents of text file *#{EL}
  • C5: File type when saving

Notes

  • The character code of the text file is UTF-8
  • In the [C5: File type when saving] you can specify the following types
    • text/plain
    • text/html
    • text/xml
    • application/json
  • When the file type is not selected, the file will be saved as text/plain

Capture

See also

Script (click to open)
  • An XML file that contains the code below is available to download
    • generator-textfile.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on


//////// START "main()" ////////
main();
function main() {

    //// == Config Retrieving & Caluculating / 工程コンフィグの参照 & 演算 ==
    const textdata = configs.get("Text_Data");
    const filename = configs.get("File_Name");
    const filedataId = configs.get("File_DataId");
    const deleteOtherFiles = configs.getObject("conf_DeleteOtherFiles");
    let contentType = configs.get("conf_ContentType");

    if (filename === "" || filename === null) {
        throw new Error("File Name is blank");
    }

    if (contentType === "" || contentType === null) {
        contentType = 'text/plain';
    }

    myFiles = retrieveData(textdata, filename, filedataId, deleteOtherFiles, contentType);
    updateData(filedataId, myFiles);
}

/**
  * テキストデータをファイル型データに変換する
  * @param {String} textdata テキストファイルの内容
  * @param {String} filename 保存ファイル名
  * @param {String} filedataId テキストファイルを保存するファイル型データ項目のデータ定義番号
  * @param {boolean} deleteOtherFiles 保存時に他のファイルを削除するかどうか
  * @param {String} contentType 保存時に他のファイルを削除するかどうか
  * @return {java.util.ArrayList<QfileView>} myFiles テキストデータをファイル型データとして追加した配列
  */
function retrieveData(textdata, filename, filedataId, deleteOtherFiles, contentType) {
    let myFiles;
    if (deleteOtherFiles === true) {
        myFiles = new java.util.ArrayList();
    } else {
        // ワークフローデータからファイル型データの配列を取得
        myFiles = engine.findDataByNumber(filedataId);// java.util.ArrayList
        if (myFiles === null) {
            myFiles = new java.util.ArrayList();
        }
    }
    if (textdata === null) {
        textdata = "";
    }

    // テキストデータをファイル型データに変換して追加した配列を返す
    myFiles.add(
        new com.questetra.bpms.core.event.scripttask.NewQfile(
            filename,
            `${contentType}; charset=UTF-8`,
            textdata
        )
    );
    return myFiles;
}

//// == Data Updating / ワークフローデータへの代入 ==
function updateData(filedataId, myFiles) {
    engine.setDataByNumber(filedataId, myFiles);
}

%d bloggers like this: