Macros/Calc/ba001

From The Document Foundation Wiki
< Macros‎ | Calc
Jump to: navigation, search

Template:TopMenu/en Template:Menu/en Template:Menu.Macros/en

Other languages:


Description

Copy content cell from Spreadsheet to other. The spreadsheet source should be open.

Basic code

 1 Sub main()
 2 DOC_NAME_SOURCE = "source.ods"
 3 SHEET_NAME_SOURCE = "source"
 4 CELL_NAME_SOURCE = "A1"
 5 
 6     <!--T:5-->
 7 doc_source = get_doc(DOC_NAME_SOURCE)
 8     sheet_source = doc_source.Sheets.getByName(SHEET_NAME_SOURCE)
 9     cell_source = sheet_source.getCellRangeByName(CELL_NAME_SOURCE)
10 
11     <!--T:6-->
12 doc = ThisComponent
13     sheet = doc.CurrentController.ActiveSheet
14     cell = sheet.getCellRangeByName(CELL_NAME_SOURCE)
15  
16     If cell_source.Type = 1 Then
17         cell.Value = cell_source.Value
18     ElseIf cell_source.Type = 2 Then
19         cell.String = cell_source.String
20     Else
21         cell.Formula = cell_source.Formula
22     End If
23 End Sub
24 
25 <!--T:7-->
26 Function get_doc(title)
27     docs = StarDesktop.getComponents()
28     enumeration = docs.createEnumeration()
29     Do While enumeration.hasMoreElements()
30         doc = enumeration.nextElement()
31         If doc.Title = title Then
32             Exit Do
33         End If
34     Loop
35     get_doc = doc
36 End Function