Excel-CSV Email Attachment (Table)

Converting the values in a Table type data item to an Excel CSV file (TSV), and sends it as email attachment.

    A. Select TABLE DATAData selectTableRequired
    B. Set THROW COL IDs (e.g. "0,1,5,3,6")Single-line inputRequired
    C. Select STRING DATA for To AddressData selectString(single line)Required
    D. Select STRING DATA for To NameData selectString(single line)Required
    E. Select STRING DATA for Email SubjectData selectString(single line)Required
    F. Set From Address (e.g. "noreply@example.com")Single-line inputRequired
    G. Set From Name (e.g. "Accounting News")Single-line inputRequired
    H. Set ReplyTo Address (e.g. "admin@example.com")Single-line inputRequired
    I. Set ReplyTo Name (e.g. "Accounting News admin")Single-line inputRequired
    J. Select STRING DATA for Email BodyData selectStringRequired
    K. Select STRING DATA for Attachment File NameData selectString(single line)Required
    -variable namevariable label
    Aconf_DataIdASelect TABLE DATA
    Bconf_ColIdsSet THROW COL IDs (e.g. "0,1,5,3,6")
    Cconf_DataIdCSelect STRING DATA for To Address
    Dconf_DataIdDSelect STRING DATA for To Name
    Econf_DataIdESelect STRING DATA for Email Subject
    Fconf_FromAddressSet From Address (e.g. "noreply@example.com")
    Gconf_FromNameSet From Name (e.g. "Accounting News")
    Hconf_ReplyToAddressSet ReplyTo Address (e.g. "admin@example.com")
    Iconf_ReplyToNameSet ReplyTo Name (e.g. "Accounting News admin")
    Jconf_DataIdJSelect STRING DATA for Email Body
    Kconf_DataIdKSelect STRING DATA for Attachment File Name
    // Email Table as Excel-CSV (ver. 20160907)
    // (c) 2016, Questetra, Inc. (the MIT License)
    
    //// == 自動工程コンフィグの参照 / Config Retrieving ==
    var dataIdA = configs.get( "conf_DataIdA" ); // (returns Number)
    var colIds = configs.get( "conf_ColIds" ) + "";
    var dataIdC = configs.get( "conf_DataIdC" ); // (returns Number)
    var dataIdD = configs.get( "conf_DataIdD" ); // (returns Number)
    var dataIdE = configs.get( "conf_DataIdE" ); // (returns Number)
    var fromAddress = configs.get( "conf_FromAddress" );
    var fromName = configs.get( "conf_FromName" );
    var replyToAddress = configs.get( "conf_ReplyToAddress" );
    var replyToName = configs.get( "conf_ReplyToName" );
    var dataIdJ = configs.get( "conf_DataIdJ" ); // (returns Number)
    var dataIdK = configs.get( "conf_DataIdK" ); // (returns Number)
    
    //// == ワークフローデータの参照 / Data Retrieving ==
    var myTable = data.get( dataIdA );
    // myTable: com.questetra.bpms.core.model.formdata.ListArray
    var toAddress = data.get( dataIdC ) + "";
    var toName = data.get( dataIdD ) + "";
    var subject = data.get( dataIdE ) + "";
    var myBody = data.get( dataIdJ ) + "";
    var myFileName = data.get( dataIdK ) + "";
    
    //// == 演算 / Calculating ==
    if( myTable !== null){
      var colIdArray = colIds.split(",");
      var numOfCols = colIdArray.length;
      var numOfRows = myTable.size() - 0; // 行(Tableの高さ)
    
      var myTsv = "";
      for( var i = 0; i < numOfRows; i++ ){
        for( var j = 0; j < numOfCols; j++ ){
          if( j != numOfCols - 1 ){
            myTsv += myTable.get(i, parseInt( colIdArray[j] ) ) + "\t";
          }else{
            myTsv += myTable.get(i, parseInt( colIdArray[j] ) ) + "\n";
          }
        }
      }
    
      emailService.begin()
        .addTo( toAddress, toName )
        .setFrom( fromAddress, fromName )
        .setReplyTo( replyToAddress, replyToName )
        .setSubject( subject )
        .setBody( myBody )
        .addAttachment( myFileName, 'text/tab-separated-values; charset=x-UTF-16LE-BOM', myTsv)
        .send();
    }
    

    • A’: Table stored in the Data Item selected at config A
    • C’: To-Address stored in the Data Item selected at config C
    • D’: To-Address Display name stored in the Data Item selected at config D
    • E’: Email subject stored in the Data Item selected at config E
    • J’: Email body text stored in the Data Item selected at config J
    • K’: Attachment filename stored in the Data Item selected in config K
    • The content of table A’ will be attached to Email as an Excel CSV file (the entities are tab-separated-values). The filename extension “.csv” is recommended.
    • “0,1,5,3,6” means changing the column order into “ID:0, ID:1, ID:5,ID:3, ID:6” before generating the CSV.
    • The SJIS version consists of comma-separated-values. Although the availability of smartphone display and data import increases, comma information misses.