Generale Examples Document Change API

Documentation •
In this article

 

Try the operations directly on an accounting file

Using this script you can test the functionality of the operations. Simply choose a document to run it on and select the script containing the operation you want to perform.




// Copyright [2020] [Banana.ch SA - Lugano Switzerland]
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// @id = ch.banana.test.read.documentpatch
// @api = 1.0
// @pubdate = 2020-10-27
// @publisher = Banana.ch SA
// @description = Read document patch files (task: app.command)
// @task = app.command
// @doctype = *
// @docproperties =
// @timeout = -1

function exec(inData) {
   if (!Banana.document)
      return "@Cancel";

   var fileContent = '';	  
   var fileName = Banana.IO.getOpenFileName("Select open file", "", "Json file (*.json);;All files (*)")
   if (fileName.length) {
      var file = Banana.IO.getLocalFile(fileName)
      //file.codecName = "latin1";  // Default is UTF-8
      fileContent = file.read();
      if (file.errorString) {
         Banana.Ui.showInformation("Read error", file.errorString);
		 return "@Cancel";
      }
   } else {
      Banana.Ui.showInformation("Info", "no file selected");
	  return "@Cancel";
   }
   
   var jsonData = {"format":"documentPatch", "error":"", "data":[]};
   try {
      jsonData = JSON.parse(fileContent);
   }
   catch (e) {
      Banana.Ui.showInformation("Info", "error parsing documentpatch");
      Banana.Ui.showText(fileContent);
      return "@Cancel";
   }

   if (!jsonData)
      return "@Cancel";
	  
   //Banana.Ui.showText("json object: " + JSON.stringify(jsonData, null, 3));
   
   return jsonData;

}
 

Complete example creating and running a DocumentChange

The following extension creates a DocumentChange, which adds and changes some rows in the Transactions table. To try this extension, you can copy and paste the code into a text file and  save it in .JS format. To install it in Banana you have to click on the Menu Extensions (command Manage extensions...) and then add the file you just created using "Add from file" button, After this procedure, the file can be run from the Menu Extensions under the new command "Modify ac2 file using document changes".



// Copyright [2020] [Banana.ch SA - Lugano Switzerland]
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// @id = ch.banana.test.create.documentpatch.ac2
// @api = 1.0
// @pubdate = 2019-10-09
// @publisher = Banana.ch SA
// @description = Modify ac2 file using document changes
// @task = app.command
// @doctype = 100.*;110.*;130.*
// @timeout = -1


function exec(inData) {

   	if (!Banana.document)
   	   	return "@Cancel";

   	var documentChange = {"format":"documentChange", "error":"", "data":[]};
	  
	//1. Appends a row to the transaction table
	var jsonDoc = transactionRowAppend();
	documentChange["data"].push(jsonDoc);


	//2. Insert a row to the transaction table at the beginning of the rows
	jsonDoc = transactionRowInsert();
	documentChange["data"].push(jsonDoc);
	
	//3. Modify all rows in the transaction table
	jsonDoc = transactionRowEdit();
	documentChange["data"].push(jsonDoc);

	// Banana.Ui.showText("json object: " + JSON.stringify(documentChange, null, 3));
	
	return documentChange;

}

function getCurrentDate() {
    var d = new Date();
    var datestring = d.getFullYear() + ("0" + (d.getMonth() + 1)).slice(-2) + ("0" + d.getDate()).slice(-2);
	return Banana.Converter.toInternalDateFormat(datestring, "yyyymmdd");
}

function getCurrentTime() {
    var d = new Date();
    var timestring = ("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2);
	return Banana.Converter.toInternalTimeFormat(timestring, "hh:mm");
}

function initDocument() {
	var jsonDoc = {};
    jsonDoc.document = {};
    jsonDoc.document.fileVersion = "1.0.0";
    jsonDoc.document.dataUnits = [];
	jsonDoc.creator = {};
    jsonDoc.creator.executionDate = getCurrentDate();
    jsonDoc.creator.executionTime = getCurrentTime();
    jsonDoc.creator.name = Banana.script.getParamValue('id');
    jsonDoc.creator.version = "1.0";
	return jsonDoc;
}

function transactionRowAppend() {

	//row operation
	var row = {};
    row.operation = {};
    row.operation.name = "add";

	//row fields
    row.fields = {};
    row.fields["Date"] = getCurrentDate();
    row.fields["Description"] = "Executed transactionRowAppend()";

	//rows
	var rows = [];
	rows.push(row);

	//table
    var dataUnitTransactions = {};
    dataUnitTransactions.nameXml = "Transactions";
    dataUnitTransactions.data = {};
    dataUnitTransactions.data.rowLists = [];
    dataUnitTransactions.data.rowLists.push({"rows":rows});
    
	//document
   	var jsonDoc = initDocument();
	jsonDoc.document.dataUnits.push(dataUnitTransactions);

	return jsonDoc;

}

function transactionRowEdit() {

	// Read the table row by row and save some values
	var rows = [];
	var table = Banana.document.table('Transactions');
	for (var i = 0; i < table.rowCount; i++) {
		var tRow = table.row(i);
		var description = tRow.value('Description');
		if (description.length<=0)
			continue;

		//row operation
		var row = {};
		row.operation = {};
		row.operation.name = "modify";
		var sequence = i.toString();
		if (sequence.length<=0)
			sequence = "0";
		row.operation.sequence = sequence;
    
		//row fields
		row.fields = {};
		row.fields["Date"] = getCurrentDate();
		row.fields["Description"] = description + " + transactionRowEdit()";
		rows.push(row);
	}
	
	//table
	var dataUnitTransactions = {};
    dataUnitTransactions.nameXml = "Transactions";
    dataUnitTransactions.data = {};
    dataUnitTransactions.data.rowLists = [];
    dataUnitTransactions.data.rowLists.push({"rows":rows});
    
	//document
   	var jsonDoc = initDocument();
	jsonDoc.document.dataUnits.push(dataUnitTransactions);

	return jsonDoc;

}

function transactionRowInsert() {

	//row operation
	var row = {};
    row.operation = {};
    row.operation.name = "add";
    row.operation.sequence = "0";

    //row fields
    row.fields = {};
    row.fields["Date"] = getCurrentDate();
    row.fields["Description"] = "Executed transactionRowInsert()";

	//rows
	var rows = [];
	rows.push(row);

	//table
	var dataUnitTransactions = {};
    dataUnitTransactions.nameXml = "Transactions";
    dataUnitTransactions.data = {};
    dataUnitTransactions.data.rowLists = [];
    dataUnitTransactions.data.rowLists.push({"rows":rows});
    
	//document
   	var jsonDoc = initDocument();
	jsonDoc.document.dataUnits.push(dataUnitTransactions);

	return jsonDoc;

}

Complete example loading and running an existing DocumentChange

The following code will ask for a DocumentChange file as input, after which it will process the operations present in the document and modify the accounting file accordingly.



// Copyright [2020] [Banana.ch SA - Lugano Switzerland]
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// @id = ch.banana.test.read.documentpatch
// @api = 1.0
// @pubdate = 2019-12-19
// @publisher = Banana.ch SA
// @description = Read document change files (task: app.command)
// @task = app.command
// @doctype = *
// @docproperties =
// @timeout = -1

//Non è utilizzato dai test ma serve per caricare manualmente una modifica in una bancartella tramite Manage Apps
function exec(inData) {
   if (!Banana.document)
      return "@Cancel";

   var fileContent = '';	  
   var fileName = Banana.IO.getOpenFileName("Select open file", "", "Json file (*.json);;All files (*)")
   if (fileName.length) {
      var file = Banana.IO.getLocalFile(fileName)
      //file.codecName = "latin1";  // Default is UTF-8
      fileContent = file.read();
      if (file.errorString) {
         Banana.Ui.showInformation("Read error", file.errorString);
		 return "@Cancel";
      }
   } else {
      Banana.Ui.showInformation("Info", "no file selected");
	  return "@Cancel";
   }
   
   var jsonData = {"format":"documentChange", "error":"", "data":[]};
   try {
      jsonData = JSON.parse(fileContent);
   }
   catch (e) {
      Banana.Ui.showInformation("Info", "error parsing documentpatch");
      Banana.Ui.showText(fileContent);
      return "@Cancel";
   }

   if (!jsonData)
      return "@Cancel";
	  
   //Banana.Ui.showText("json object: " + JSON.stringify(jsonData, null, 3));
   
   return jsonData;

}
Tell us how we can help you better
If the information on this page is not what you're looking for, is not clear enough, or is not up-to-date, let us know.

Share this article: Twitter | Facebook | LinkedIn | Email