Converter (Table type data to Excel-CSV File)
Stores the values of a Table-type Data Item in a File-type Data Item as an Excel compatible CSV. Files are added and existing files are not affected.
2020-11-12 (C) Questetra, Inc. (MIT License)
Configs
  • A: Target Table type data item *
  • B: Saving file name * #{EL}
  • C: File type data item to save CSV *
Script
main();
function main() {
  //// == Config Retrieving / 工程コンフィグの参照 ==
  const tableId = configs.get("Table_DataId");
  const filename = configs.get("File_Name");
  const fileId = configs.get("File_DataId");

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

  //// == Data Retrieving / ワークフローデータの参照 ==
  const myTable = engine.findDataByNumber(tableId);
  // com.questetra.bpms.core.model.formdata.ListArray
  let myFiles = engine.findDataByNumber(fileId); // java.util.ArrayList
  if (myFiles === null) {
    myFiles = new java.util.ArrayList();
  }

  let myTsv = "";

  if (myTable !== null) {
    myTsv = tableToTsv(myTable);
  }

  myFiles.add(
    new com.questetra.bpms.core.event.scripttask.NewQfile(
      filename,
      "text/tab-separated-values; charset=x-UTF-16LE-BOM",
      myTsv
    )
  );

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

/**
 * テーブル型データ を tsv に変換する
 * @param {com.questetra.bpms.core.model.formdata.ListArray} myTable テーブル型データ
 * @return {String} mytsv 変換後の tsv 形式の文字列
 */
function tableToTsv(myTable) {
  //// == Calculating / 演算 ==
  const numOfRows = myTable.size() - 0; // 行(Tableの高さ)
  const numOfCols = myTable.getRow(0).size(); // 列(Tableの幅)
  let mytsv = "";

  for (let i = 0; i < numOfRows; i++) {
    for (let j = 0; j < numOfCols; j++) {
      mytsv += myTable.get(i, j);
      if (j != numOfCols - 1) {
        mytsv += "\t";
      }
    }
    if (i != numOfRows - 1) {
      mytsv += "\n";
    }
  }

  return mytsv;
}

Download

Capture

Notes

  1. The file entity consists of tab-separated-values (utf-16le-bom)

See also

1 thought on “Converter (Table type data to Excel-CSV File)”

  1. Pingback: Converter (Table to Excel-CSV FILE sjis) – Questetra Support

Comments are closed.

%d bloggers like this: