Neste artigo
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 documentChange".
// @id = ch.banana.test.create.documentchange.ac2
// @api = 1.0
// @pubdate = 2019-10-09
// @publisher = Banana.ch SA
// @description = Modify ac2 file using documentChange
// @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.
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 "Read document change files (task: app.command)".
// @id = ch.banana.test.read.documentchange
// @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
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 documentChange");
Banana.Ui.showText(fileContent);
return "@Cancel";
}
if (!jsonData)
return "@Cancel";
//Banana.Ui.showText("json object: " + JSON.stringify(jsonData, null, 3));
return jsonData;
}