summaryrefslogtreecommitdiff
path: root/scripting/examples/beanshell/MemoryUsage/memusage.bsh
blob: fd64eb525d4fcf2533250372b671debbd4920420 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.Type;
import com.sun.star.lang.XComponent;
import com.sun.star.container.XIndexAccess;
import com.sun.star.sheet.XSpreadsheetDocument;
import com.sun.star.sheet.XSpreadsheet;
import com.sun.star.script.provider.XScriptContext;

addEntry(date, total, free) {

    // The XSCRIPTCONTEXT variable is of type XScriptContext and is available to
    // all BeanShell scripts executed by the Script Framework
    comp = XSCRIPTCONTEXT.getDocument();

    // get the XSpreadsheetDocument interface which allows us to work on the 
    // document
    doc = (XSpreadsheetDocument)
        UnoRuntime.queryInterface(XSpreadsheetDocument.class, comp);

    // get the XIndexAccess interface from the document, so that we can 
    // get the first sheet
    index = (XIndexAccess)
        UnoRuntime.queryInterface(XIndexAccess.class, doc.getSheets());

    // get the XSpreadsheet interface corresponding to the first (zero-th)
    // sheet in the document
    sheet = (XSpreadsheet) AnyConverter.toObject(
        new Type(com.sun.star.sheet.XSpreadsheet.class), index.getByIndex(0));

    // set the value in the cells
    sheet.getCellByPosition(0, 1).setValue(total - free);
    sheet.getCellByPosition(1, 1).setValue(free);
    sheet.getCellByPosition(2, 1).setValue(total);
    sheet.getCellByPosition(0, 2).setFormula(date);
}

runtime = Runtime.getRuntime();
generator = new Random();
date = new Date();

// allocate a random number of bytes so that the data changes
len = (int)(generator.nextFloat() * runtime.freeMemory() / 5);
bytes = new byte[len];
//update the entry in the spreadsheet
addEntry(date.toString(), runtime.totalMemory(), runtime.freeMemory());

return 0;