Develop an Import Extensions

Documentación •
En este artículo

Most software have the choice to export data in a predefined format and don't give you the chance to create an export format specific to Banana Accounting.

For this cases it is possible to program an Import Extension that that converts data from a proprietary format to a format that is accepted by Banana.

Import Extensions read a custom format and convert in an import format suitable for using with the command "Import to accounting".

Create an Import Extensions for converting from other formats

The Import Extensions can be used within the Command Action->Import into Accounting.

Extension Attributes

An Import extension has the Extension attribute @task of type "import.":

exec() function

Banana load an extension and call the Exec(inText) function of an Import Extention:

  • The exec( inText) the argument receive the row data that is read from the file or clipboard.
  • The exec() function should return a value that contains the converted data that is to be imported. It can be of type:
    • TSV (Tab separated Value)
      • The first line should contains the column names.
        Columns names varies dependent on the table or functionality.
        See Import data from a txt file.
      • The other lines should contain the data to be imported.
    • JSon in the the DocumentChange API Format.

Convert to a tab separated text

Imports Extensions are JavaScript program that import data to a specific table.
The import with tab separated text only allow to add rows to a table:

  •  

Import Extensions have:

  • the attribute @task defined as one of the import for example //@task = import.transactions (for more information, see Apps attributes documentation)
  • the attribute @outputformat defines the format of the imported data:
    • For a Double-entry accounting use the value //@outputformat = tablewithheaders.
    • For an Income/Expenses accounting use the value //@outputformat = transactions.simple.
  • The parameter in the function exec contains the import data (the content of the file specified in the input box)
  • You can specify that the data is read from the file specified on the input box or that the user can select the file with "// @inputdatasource = openfiledialog"  
  • The import text is returned as a String in the function exec with the return statement
// @api = 1.0
// @id = ch.banana.scripts.import.creditsuisse
// @description = Credit Suisse bank (*.csv)
// @task = import.transactions
// @doctype = nodocument
// @publisher = Banana.ch SA
// @pubdate = 2015-06-21
// @outputformat = transactions.simple
// @inputdatasource = openfiledialog
// @inputfilefilter = Text files (*.txt *.csv);;All files (*.*)
// @inputfilefilter.de = Text (*.txt *.csv);;Alle Dateien (*.*)
// @inputfilefilter.fr = Texte (*.txt *.csv);;Tous (*.*)
// @inputfilefilter.it = Testo (*.txt *.csv);;Tutti i files (*.*)

/**
* Parse the data and return the data to be imported as a tab separated file.
*/
function exec(inText) {
  
  // parse the inText and set to outText
  // in the return text the data is tab separated
  
  var outText = "";
  outText += "Date\tDescription\tIncome\tExpenses\n";
  outText += "2015-01-01\tIncome text\t100.25\t\n";
  outText += "2015-01-02\tExpense text\t\t73.50\n";  
  
  return outText;
}

Import with DocumentChange

You can directly import the data into accounting using the DocumentChange API. Parse the information, define the changes to be made to the document and return the JSON Object.

With the DocumentChange API you can add, modify or remove line from any tables. You can also add or modify columns.

In the example below, we import two contacts from a .csv file using DocumentChange.

// @api = 1.0
// @id = ch.banana.switzerland.pf.smartbusinees.import.csv.js
// @description = Import PostFinance SmartbBusiness data (*.csv)
// @task = import.transactions
// @doctype = nodocument
// @publisher = Banana.ch SA
// @pubdate = 2021-09-22
// @outputformat = tablewithheaders
// @inputdatasource = openfiledialog
// @inputfilefilter = Text files (*.txt *.csv);;All files (*.*)

/**
* Parse the data and return the data ad DocumentChange.
*/
function exec(inText) {
  
  // parse the inText and set the DocumentChange.
  
 var docChange=
 {
    "format":"documentChange",
    "error":"",
    "data":[
       {
          "document":{
             "dataUnits":[
                {
                    "nameXml": "Contacts",
                    "data": {
                      "rowLists": [
                        {
                          "rows": [
                            {
                              "operation": {
                                "name": "add"
                              },
                              "fields": {
                                "RowId": "8247",
                                "FirstName": "John",
                                "FamilyName": "Smith",
                                "Gender": "M",
                                "PhoneMain": "07823615",
                                "EmailWork": "info@example.ch",
                                "CountryCode": "CH"
                              }
                            },
                            {
                              "operation": {
                                "name": "add"
                              },
                              "fields": {
                                "RowId": "4",
                                "FirstName": "Kimberely",
                                "FamilyName": "Grant",
                                "Gender": "F",
                                "PhoneMain": "076892345",
                                "EmailWork": "info2@example2.ch",
                                "CountryCode": "CH"
                              }
                            }
                          ]
                        }
                      ]
                    }
                }
             ]
          }
       }
    ]
  }

 //return the DocumentChange Object
 return docChange;
}

Help us improve the documentation

We welcome feedback on how to improve this page.

Tell us what theme needs a better explanation or how to clarify a topic.

Share this article: Twitter | Facebook | LinkedIn | Email