Cell操作(2)
[ Column・Row(行・列) ]
[ Hyper Link ]
[ Array ]
Sheet
File
Graph Chart作成
印刷操作
Cell操作(2)
[ Column・Row(行・列) ]
#!
#coding: utf-8
# python Marco
import uno
import sys, traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
from com.sun.star.beans import PropertyValue
class cCellVertJust():
from com.sun.star.table.CellVertJustify import (STANDARD, TOP, CENTER, BOTTOM)
def omsgbox(oMessage='',oBtnType=1,oTitle='Title',oMsgType='messbox'):
"""shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, 1, 'Title', oMessage)
return msgbox.execute()
def oTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByName("Sheet1")
oRows = oSheet.getRows()
oRows.insertByIndex(2,3) # ←3行目から3行挿入
oDisp = ' Success\n ( Python )'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,'LO7.0.4.2')
#!
#coding: utf-8
# python Marco
import uno
import sys, traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
from com.sun.star.beans import PropertyValue
class cCellVertJust():
from com.sun.star.table.CellVertJustify import (STANDARD, TOP, CENTER, BOTTOM)
def omsgbox(oMessage='',oBtnType=1,oTitle='Title',oMsgType='messbox'):
"""shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, 1, 'Title', oMessage)
return msgbox.execute()
def oTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByName("Sheet1")
oRows = oSheet.getColumns()
oRows.insertByIndex(2,3) # ←C列目から3列挿入
oDisp = ' Success\n ( Python )'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,'LO7.0.4.2')
#!
#coding: utf-8
# python Marco
import uno
import sys, traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
from com.sun.star.beans import PropertyValue
class cCellVertJust():
from com.sun.star.table.CellVertJustify import (STANDARD, TOP, CENTER, BOTTOM)
def omsgbox(oMessage='',oBtnType=1,oTitle='Title',oMsgType='messbox'):
"""shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, 1, 'Title', oMessage)
return msgbox.execute()
def oTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByName("Sheet1")
oRows = oSheet.getRows()
oRows.removeByIndex(1,3) # ←2行目から3行削除
oDisp = ' Success\n ( Python )'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,'LO7.0.4.2')
#!
#coding: utf-8
# python Marco
import uno
import sys, traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
from com.sun.star.beans import PropertyValue
class cCellVertJust():
from com.sun.star.table.CellVertJustify import (STANDARD, TOP, CENTER, BOTTOM)
def omsgbox(oMessage='',oBtnType=1,oTitle='Title',oMsgType='messbox'):
"""shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, 1, 'Title', oMessage)
return msgbox.execute()
def oTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByName("Sheet1")
oRows = oSheet.getColumns()
oRows.removeByIndex(1,2) # ←B列目から2列削除
oDisp = ' Success\n ( Python )'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,'LO7.0.4.2')


#!
#coding: utf-8
# python Marco
import uno
import sys, traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
from com.sun.star.beans import PropertyValue
class cCellVertJust():
from com.sun.star.table.CellVertJustify import (STANDARD, TOP, CENTER, BOTTOM)
def omsgbox(oMessage='',oBtnType=1,oTitle='Title',oMsgType='messbox'):
"""shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, 1, oTitle, oMessage)
return msgbox.execute()
def oTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByName("Sheet1")
Unit_Hieght = oSheet.getRows().getByIndex(0).Height
Unit_Width = oSheet.getColumns().getByIndex(1).Width
oDisp = '1行目の行高さ = ' + str(Unit_Hieght) + '\n' + 'B列の列幅 = ' + str(Unit_Width)
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,'LO7.0.4.2')
#!
#coding: utf-8
# python Marco
import uno
import sys, traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
from com.sun.star.beans import PropertyValue
class cCellVertJust():
from com.sun.star.table.CellVertJustify import (STANDARD, TOP, CENTER, BOTTOM)
def omsgbox(oMessage='',oBtnType=1,oTitle='Title',oMsgType='messbox'):
"""shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, 1, oTitle, oMessage)
return msgbox.execute()
def oTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByName("Sheet1")
Unit_Hieght = oSheet.getRows().getByIndex(0).Height
Unit_Width = oSheet.getColumns().getByIndex(1).Width
oDisp = '[変更前]\n' + '1行目の行高さ = ' + str(Unit_Hieght) + '\n' + 'B列の列幅 = ' + str(Unit_Width)
# 1行目高さ、B列幅を変更
oSheet.getRows().getByIndex(0).Height = 1000 # unit: 1/100 mm
oSheet.getColumns().getByIndex(1).Width = 500 # unit: 1/100 mm
Unit_Hieght = oSheet.getRows().getByIndex(0).Height
Unit_Width = oSheet.getColumns().getByIndex(1).Width
oDisp = oDisp + '\n\n[変更後]\n' + '1行目の行高さ = ' + str(Unit_Hieght) + '\n' + 'B列の列幅 = ' + str(Unit_Width)
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,'LO7.0.4.2')

#!
#coding: utf-8
# python Marco
import uno
import sys, traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
from com.sun.star.beans import PropertyValue
class cCellVertJust():
from com.sun.star.table.CellVertJustify import (STANDARD, TOP, CENTER, BOTTOM)
def omsgbox(oMessage='',oBtnType=1,oTitle='Title',oMsgType='messbox'):
"""shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, 1, oTitle, oMessage)
return msgbox.execute()
def oTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByName("Sheet1")
# 1行目行高、B列幅を最適化
oSheet.getRows().getByIndex(0).OptimalHeight = True
oSheet.getColumns().getByIndex(1).OptimalWidth = True
oDisp = "Success"
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,'LO7.0.4.2')

#!
#coding: utf-8
# python Marco
import uno
import sys, traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
from com.sun.star.beans import PropertyValue
class cCellVertJust():
from com.sun.star.table.CellVertJustify import (STANDARD, TOP, CENTER, BOTTOM)
def omsgbox(oMessage='',oBtnType=1,oTitle='Title',oMsgType='messbox'):
"""shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, 1, oTitle, oMessage)
return msgbox.execute()
def oTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByName("Sheet1")
# 1行目、B列を非表示化
oSheet.getRows().getByIndex(0).IsVisible = False
oSheet.getColumns().getByIndex(1).IsVisible = False
omsgbox('非表示化',1,'LO7.0.4.2')
# 1行目、B列を表示化
oSheet.getRows().getByIndex(0).IsVisible = True
oSheet.getColumns().getByIndex(1).IsVisible = True
oDisp = '表示化'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,'LO7.0.4.2')

#!
#coding: utf-8
# python Marco
import uno
import sys, traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
from com.sun.star.beans import PropertyValue
class cCellVertJust():
from com.sun.star.table.CellVertJustify import (STANDARD, TOP, CENTER, BOTTOM)
def omsgbox(oMessage='',oBtnType=1,oTitle='Title',oMsgType='messbox'):
"""shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, 1, oTitle, oMessage)
return msgbox.execute()
def oTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet =oDoc.getSheets().getByName('sheet1')
#
sCol = 1 #B列
sRow = 1 #2行目
eCol = 3 #D列
eRow = 2 #3行目
oRange = oSheet.getCellRangeByPosition(sCol, sRow, eCol, eRow)
# oRange = oSheet.getRowRangeByName('B2:D3') # こちらでも同じ
objRow = oRange.getRows()
objCol = oRange.getColumns()
# 2~3行目、B~D列を非表示化
objRow.IsVisible = False
objCol.IsVisible = False
omsgbox('2~3行目、B~D列を非表示化',1,'LO7.0.4.2')
# 2~3行目、B~D列を表示化
objRow.IsVisible = True
objCol.IsVisible = True
oDisp = '2~3行目、B~D列を表示化'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,'LO7.0.4.2')
[ Hyper Link ]
#!
#coding: utf-8
# python Marco
import uno
import sys, traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
def omsgbox(oMessage='',oBtnType=1,oTitle='Title',oMsgType='messbox'):
"""shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, 1, 'Title', oMessage)
return msgbox.execute()
def Test():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByName('sheet1')
oCell = oSheet.getCellRangeByName('A1')
oUrl = 'http://openoffice3.web.fc2.com/'
oText = u'OSSでいこう!!'
# new URL field
oHyperLink = oDoc.createInstance('com.sun.star.text.textfield.URL')
oHyperLink.URL = oUrl
oHyperLink.Representation = oText
oHyperLink.TargetFrame = '_blank'
oTextCursor = oCell.createTextCursor()
oCell.insertTextContent( oTextCursor, oHyperLink, False )
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1)
[ Array ]
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
def omsgbox(oMessage='',oBtnType=1,oTitle='Title',MsgType=MESSAGEBOX):
# """shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByName('sheet1')
oRange = oSheet.getCellRangeByName('A1:B6')
oAllData = oRange.getDataArray()
oDisp = u'[ Data一括取得 ]'
for i in oAllData:
oDisp = oDisp + '\n\t' + str(i)
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1)
Sheet
# -*- coding: utf-8 -*-
import uno
import unohelper
import os.path
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
def omsgbox(oMessage='',oBtnType=1,oTitle='Title',MsgType=MESSAGEBOX):
# """shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
ofile= os.path.abspath('c:\\temp\\oootest.ods')
oURL = unohelper.systemPathToFileUrl(ofile)
#
desktop = XSCRIPTCONTEXT.getDesktop()
oDoc = desktop.loadComponentFromURL( oURL,"_blank", 0, () )
#
oSheet = oDoc.getSheets().getByIndex(0)
#
oSName = oSheet.Name
oDisp = 'SheetName : ' + oSName
#
omsgbox(desktop,oDisp)
except:
pass
# -*- coding: utf-8 -*-
import uno
import unohelper
import os.path
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
def omsgbox(oMessage='',oBtnType=1,oTitle='Title',MsgType=MESSAGEBOX):
# """shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
ofile= os.path.abspath('c:\\temp\\oootest.ods')
oURL = unohelper.systemPathToFileUrl(ofile)
#
desktop = XSCRIPTCONTEXT.getDesktop()
oDoc = desktop.loadComponentFromURL( oURL,"_blank", 0, () )
#
oSheet = oDoc.getSheets().getByIndex(0)
# get Sheet Name
oSName1 = oSheet.Name
oDisp = 'SheetName : ' + oSName1
oDisp = oDisp + "\n"
oDisp = oDisp + u'から' + "\n"
# set sheet name
oSheet.Name = 'macro1'
# confirm
oSName2 = oSheet.Name
oDisp = oDisp + 'SheetName : ' + oSName2
#
omsgbox(desktop,oDisp)
except:
pass
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
#
def omsgbox(oMessage='',oBtnType=1,oTitle='Title'):
# """shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, oBtnType, oTitle, oMessage)
return msgbox.execute()
def Test():
try:
doc = XSCRIPTCONTEXT.getDocument()
names = doc.Sheets.ElementNames
oDisp = str(names)
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1)
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
#
def omsgbox(oMessage='',oBtnType=1,oTitle='Title'):
# """shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, oBtnType, oTitle, oMessage)
return msgbox.execute()
def Test():
try:
doc = XSCRIPTCONTEXT.getDocument()
exists1 = 'sheet1' in doc.Sheets
exists2 = 'Sheet1' in doc.Sheets
exists3 = 'Sheet9' in doc.Sheets
oDisp = ' sheet1 : ' + str(exists1) +'\n Sheet1 : ' + str(exists2) +'\n Sheet9 : ' + str(exists3)
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1)
File
# -*- coding: utf-8 -*-
import uno
def load():
desktop = XSCRIPTCONTEXT.getDesktop()
doc =desktop.loadComponentFromURL( "private:factory/scalc","_blank", 0, () )
# Uno の sequence は Python ではタプル、 Basic だと Array
# -*- coding: utf-8 -*-
import uno
import unohelper
import os.path
def oMacroTest():
try:
ofile= os.path.abspath('c:\\temp\\oootest.ods')
oURL = unohelper.systemPathToFileUrl(ofile)
#
desktop = XSCRIPTCONTEXT.getDesktop()
desktop.loadComponentFromURL( oURL,"_blank", 0, () )
except:
pass
# -*- coding: utf-8 -*-
from com.sun.star.beans import PropertyValue
import uno
import unohelper
import os.path
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
def omsgbox(oMessage='',oBtnType=1,oTitle='Title',MsgType=MESSAGEBOX):
# """shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
ofile= os.path.abspath('c:\\temp\\oootest.ods')
oURL = unohelper.systemPathToFileUrl(ofile)
#
desktop = XSCRIPTCONTEXT.getDesktop()
oDoc = desktop.loadComponentFromURL( oURL,"_blank", 0, () )
#
oSheet = oDoc.getSheets().getByIndex(0)
#
oCell = oSheet.getCellByPosition(0,0)
oCell.String = 'Store Success'
#
p = PropertyValue()
p.Name = 'Overwrite'
p.Value = True
properties = (p,)
oDoc.storeAsURL( oURL, properties)
oDoc.dispose()
#
omsgbox(desktop,"Success")
except:
pass
Graph Chart作成
#!
#coding: utf-8
# python Marco
import uno
import sys, traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
def omsgbox(oMessage='',oBtnType=1,oTitle='Title'):
# """shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByIndex(0)
for i in range(5):
oCellA = oSheet.getCellByPosition(0,i)
oCellB = oSheet.getCellByPosition(1,i)
oCellA.Value = i + 1
oCellB.Formula = '=A' + str(i+1) + '*10'
# Set Data Range
oRange = oSheet.getCellRangeByName("A1:B5").getRangeAddress()
# Set Sheet Index
oRangeAddress = uno.createUnoStruct('com.sun.star.table.CellRangeAddress')
#Set Sheet Index
oRangeAddress.Sheet = oRange.Sheet # ← 大文字小文字の区別あり
# Set X axis Data
oRangeAddress.StartColumn = oRange.StartColumn
oRangeAddress.EndColumn = oRange.EndColumn
# Set Y axis Data
oRangeAddress.StartRow = oRange.StartRow
oRangeAddress.EndRow = oRange.EndRow
#
# oRangeAddressのデータをList化
oDataRng = []
oDataRng.append(oRangeAddress)
#
# Set Size & Position of Chart
oRect = uno.createUnoStruct('com.sun.star.awt.Rectangle')
oRect.Height = 5000 # Unit : 1/100mm
oRect.Width = 6000 # Unit : 1/100mm
oRect.X = 1800 # Unit : 1/100mm
oRect.Y = 100 # Unit : 1/100mm
#
# 同名Chart削除
oTitle='CalcChart2'
oCharts=oSheet.getCharts()
if oCharts.hasByName(oTitle):
oCharts.removeByName(oTitle) # ← 大文字小文字の区別あり
#
# Add new Chart
oCharts.addNewByName(oTitle, oRect, oDataRng, False, False)
#
# Get newly created chart
oChart = oCharts.getByName(oTitle).getEmbeddedObject()
#
oDisp = ' Success\n ( Python )'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,'LO6.2.4.2(x64)')
#!
#coding: utf-8
# python Marco
import uno
import sys, traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
def omsgbox(oMessage='',oBtnType=1,oTitle='Title'):
# """shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByIndex(0)
oRowNum =10
for i in range(oRowNum + 1):
oCellA = oSheet.getCellByPosition(0,i)
oCellB = oSheet.getCellByPosition(1,i)
oCellA.Value = -5 + i
oCellB.Formula = '=A' + str(i+1) + '^2'
# Set Data Range
oCellRng = 'A1:B' + str(oRowNum + 1)
oRange = oSheet.getCellRangeByName(oCellRng).getRangeAddress()
# Set Sheet Index
oRangeAddress = uno.createUnoStruct('com.sun.star.table.CellRangeAddress')
#Set Sheet Index
oRangeAddress.Sheet = oRange.Sheet # ← 大文字小文字の区別あり
# Set X axis Data
oRangeAddress.StartColumn = oRange.StartColumn
oRangeAddress.EndColumn = oRange.EndColumn
# Set Y axis Data
oRangeAddress.StartRow = oRange.StartRow
oRangeAddress.EndRow = oRange.EndRow
#
# oRangeAddressのデータをList化
oDataRng = []
oDataRng.append(oRangeAddress)
#
# Set Size & Position of Chart
oRect = uno.createUnoStruct('com.sun.star.awt.Rectangle')
oRect.Height = 5000 # Unit : 1/100mm
oRect.Width = 6000 # Unit : 1/100mm
oRect.X = 1800 # Unit : 1/100mm
oRect.Y = 100 # Unit : 1/100mm
#
# 同名Chart削除
oTitle='CalcChart2'
oCharts=oSheet.getCharts()
if oCharts.hasByName(oTitle):
oCharts.removeByName(oTitle) # ← 大文字小文字の区別あり
#
# Add new Chart
oCharts.addNewByName(oTitle, oRect, oDataRng, False, False)
#
# Get newly created chart
oChart = oCharts.getByName(oTitle).getEmbeddedObject()
#
# Diagram of the css.chart2
oDiagram = oChart.getFirstDiagram()
# Create template and set to it
oChartTypeManager = oChart.getChartTypeManager()
oChartTypeTemplate = oChartTypeManager.createInstance('com.sun.star.chart2.template.ScatterLineSymbol')
oChartTypeTemplate.changeDiagram(oDiagram)
#
oDisp = ' Success\n ( Python )'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,'LO6.2.4.2(x64)')
#
# Note
# Templateの種類は BasicのCodeを参照
#!
#coding: utf-8
# python Marco
import uno
import sys, traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
def omsgbox(oMessage='',oBtnType=1,oTitle='Title'):
# """shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByIndex(0)
oRowNum =10
for i in range(oRowNum + 1):
oCellA = oSheet.getCellByPosition(0,i)
oCellB = oSheet.getCellByPosition(1,i)
oCellA.Value = -5 + i
oCellB.Formula = '=A' + str(i+1) + '^2'
# Set Data Range
oCellRng = 'A1:B' + str(oRowNum + 1)
oRange = oSheet.getCellRangeByName(oCellRng).getRangeAddress()
# Set Sheet Index
oRangeAddress = uno.createUnoStruct('com.sun.star.table.CellRangeAddress')
#Set Sheet Index
oRangeAddress.Sheet = oRange.Sheet # ← 大文字小文字の区別あり
# Set X axis Data
oRangeAddress.StartColumn = oRange.StartColumn
oRangeAddress.EndColumn = oRange.EndColumn
# Set Y axis Data
oRangeAddress.StartRow = oRange.StartRow
oRangeAddress.EndRow = oRange.EndRow
#
# oRangeAddressのデータをList化
oDataRng = []
oDataRng.append(oRangeAddress)
#
# Set Size & Position of Chart
oRect = uno.createUnoStruct('com.sun.star.awt.Rectangle')
oRect.Height = 5000 # Unit : 1/100mm
oRect.Width = 6000 # Unit : 1/100mm
oRect.X = 1800 # Unit : 1/100mm
oRect.Y = 100 # Unit : 1/100mm
#
# 同名Chart削除
oTitle='CalcChart2'
oCharts=oSheet.getCharts()
if oCharts.hasByName(oTitle):
oCharts.removeByName(oTitle) # ← 大文字小文字の区別あり
#
# Add new Chart
oCharts.addNewByName(oTitle, oRect, oDataRng, False, False)
#
# Get newly created chart
oChart = oCharts.getByName(oTitle).getEmbeddedObject()
#
# Diagram of the css.chart2
oDiagram = oChart.getFirstDiagram()
#
# Create template and set to it
oChartTypeManager = oChart.getChartTypeManager()
oChartTypeTemplate = oChartTypeManager.createInstance('com.sun.star.chart2.template.ThreeDScatter')
oChartTypeTemplate.changeDiagram(oDiagram)
#
# 変更前を確認
omsgbox(u'Before Change',1)
#
# Chart Areaの変更
# Get Title Chart
oChart_Line = oCharts.getByName(oTitle).EmbeddedObject
oChart_Line.HasMainTitle = True
oChart_Line.Title.String = oTitle
#
# Show Title of Axis
oChart_Line.Diagram.HasXAxisTitle = True # ← 大文字小文字の区別あり
oChart_Line.Diagram.XAxisTitle.String = 'Data'
oChart_Line.Diagram.HasYAxisTitle = True
oChart_Line.Diagram.YAxisTitle.String = 'Number of Cases'
#
# Set align of scale) marks on X axis
oChart_Line.Diagram.XAxis.TextBreak = False
oChart_Line.Diagram.XAxis.TextRotation = 2700 # Unit: 1/100th of degree
#
# Set propeteries of Chart Area
oArea = oChart_Line.getArea()
oArea.FillStyle = uno.Enum('com.sun.star.drawing.FillStyle', 'SOLID')
oArea.FillBackground = True
oArea.FillColor = 0xff00ff # 背景色⇒ RGBの16進数6桁で表示 0x + RGB(255,0,255)⇒ 0xff00ff
oArea.FillTransparence = '80%'
#
# Line Properties
oArea.LineStyle = uno.Enum('com.sun.star.drawing.LineStyle', 'SOLID')
oArea.LineWidth = 50
oArea.LineColor = 0x0000ff
oArea.LineTransparence = '50%'
#
# Diagram Area
oDgn = oChart_Line.getDiagram().getWall()
oDgn.FillStyle = uno.Enum('com.sun.star.drawing.FillStyle', 'SOLID')
oDgn.FillBackground = True
oDgn.FillColor = 0xff0000
oDgn.FillTransparence = '80%'
oDgn.LineStyle = uno.Enum('com.sun.star.drawing.LineStyle', 'SOLID')
oDgn.LineWidth = 50
oDgn.LineColor = 0x00ff00
oDgn.LineTransparence = '50%'
#
oDisp = ' Success\n ( Python )'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,'LO6.2.4.2(x64)')
#
#

#!
#coding: utf-8
# python Marco
import uno
import sys, traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
def omsgbox(oMessage='',oBtnType=1,oTitle='Title'):
# """shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByIndex(0)
oRowNum =10
for i in range(oRowNum + 1):
oCellA = oSheet.getCellByPosition(0,i)
oCellB = oSheet.getCellByPosition(1,i)
oCellA.Value = -5 + i
oCellB.Formula = '=A' + str(i+1) + '^3'
# Set Data Range
oCellRng = 'A1:B' + str(oRowNum + 1)
oRange = oSheet.getCellRangeByName(oCellRng).getRangeAddress()
# Set Sheet Index
oRangeAddress = uno.createUnoStruct('com.sun.star.table.CellRangeAddress')
#Set Sheet Index
oRangeAddress.Sheet = oRange.Sheet # ← 大文字小文字の区別あり
# Set X axis Data
oRangeAddress.StartColumn = oRange.StartColumn
oRangeAddress.EndColumn = oRange.EndColumn
# Set Y axis Data
oRangeAddress.StartRow = oRange.StartRow
oRangeAddress.EndRow = oRange.EndRow
#
# oRangeAddressのデータをList化
oDataRng = []
oDataRng.append(oRangeAddress)
#
# Set Size & Position of Chart
oRect = uno.createUnoStruct('com.sun.star.awt.Rectangle')
oRect.Height = 5000 # Unit : 1/100mm
oRect.Width = 6000 # Unit : 1/100mm
oRect.X = 1800 # Unit : 1/100mm
oRect.Y = 100 # Unit : 1/100mm
#
# 同名Chart削除
oTitle='CalcChart2'
oCharts=oSheet.getCharts()
if oCharts.hasByName(oTitle):
oCharts.removeByName(oTitle) # ← 大文字小文字の区別あり
#
# Add new Chart
oCharts.addNewByName(oTitle, oRect, oDataRng, False, False)
#
# Get newly created chart
oChart = oCharts.getByName(oTitle).getEmbeddedObject()
#
# Diagram of the css.chart2
oDiagram = oChart.getFirstDiagram()
#
# Create template and set to it
oChartTypeManager = oChart.getChartTypeManager()
oChartTypeTemplate = oChartTypeManager.createInstance('com.sun.star.chart2.template.ScatterLineSymbol')
oChartTypeTemplate.changeDiagram(oDiagram)
#
# 変更前を確認
omsgbox(u'Before Change',1)
#
# Chart Areaの変更
# Get Title Chart
oChart_Line = oCharts.getByName(oTitle).EmbeddedObject
oChart_Line.HasMainTitle = True
oChart_Line.Title.String = oTitle
#
# Show Title of Axis
oChart_Line.Diagram.HasXAxisTitle = True # ← 大文字小文字の区別あり
oChart_Line.Diagram.XAxisTitle.String = 'Data'
oChart_Line.Diagram.HasYAxisTitle = True
oChart_Line.Diagram.YAxisTitle.String = 'Number of Cases'
#
# Set align of scale) marks on X axis
oChart_Line.Diagram.XAxis.TextBreak = False
oChart_Line.Diagram.XAxis.TextRotation = 2700 # Unit: 1/100th of degree
#
# Set propeteries of Chart Area
oArea = oChart_Line.getArea()
oArea.FillStyle = uno.Enum('com.sun.star.drawing.FillStyle', 'SOLID')
oArea.FillBackground = True
oArea.FillColor = 0xff00ff # 背景色⇒ RGBの16進数6桁で表示 0x + RGB(255,0,255)⇒ 0xff00ff
oArea.FillTransparence = '80%'
#
# Line Properties
oArea.LineStyle = uno.Enum('com.sun.star.drawing.LineStyle', 'SOLID')
oArea.LineWidth = 50
oArea.LineColor = 0x0000ff
oArea.LineTransparence = '50%'
#
# Diagram Area
oDgn = oChart_Line.getDiagram().getWall()
oDgn.FillStyle = uno.Enum('com.sun.star.drawing.FillStyle', 'SOLID')
oDgn.FillBackground = True
oDgn.FillColor = 0xff0000
oDgn.FillTransparence = '80%'
oDgn.LineStyle = uno.Enum('com.sun.star.drawing.LineStyle', 'SOLID')
oDgn.LineWidth = 50
oDgn.LineColor = 0x00ff00
oDgn.LineTransparence = '50%'
#
#Properties of Diagram
#
omsgbox('Data Sereisのの変更',1)
#
oDiagram = oChart_Line.getFirstDiagram()
oCooSys = oDiagram.getCoordinateSystems()
oCoods = oCooSys[0] # 上記で作成したChartには1つの座標軸しかないので
#
oChartTypes = oCoods.getChartTypes() # chart type one by one
oChartType = oChartTypes[0]
#
# Data Seriesの取得
oDataSeriesList = oChartType.getDataSeries()
#
# Data Sereisの色変更
oDataSeriesList[0].Color = 0xbb00aa # bb = 187, aa = 170 ⇒ RGB(187,0,170)
#
oDisp = ' Success\n ( Python )'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,'LO6.2.4.2(x64)')
#
#

印刷操作
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
#
def omsgbox(oMessage='',oBtnType=1,oTitle='Title'):
# """shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, oBtnType, oTitle, oMessage)
return msgbox.execute()
#
def oTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByIndex(0)
oSheet.getRows().getByIndex(4).IsStartOfNewPage = True
omsgbox('4行目の下に改Page(行)設定',1,'改Page')
#
oSheet.getRows().getByIndex(4).IsStartOfNewPage = False
omsgbox('4行目の下の改Page(行)解除',1,'改Page')
#
oSheet.getColumns().getByIndex(1).IsStartOfNewPage = True
omsgbox('B列の左に改Page(列)設定',1,'改Page')
#
oSheet.getColumns().getByIndex(1).IsStartOfNewPage = False
omsgbox('B列の左の改Page(列)解除',1,'改Page')
#
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,'LO6.4.3.2(x64)')

#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
def omsgbox(oMessage='',oBtnType=1,oTitle='Title'):
# """shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oCtrl = oDoc.getCurrentController()
oSelRange = oCtrl.getActiveSheet()
oSelRow = oSelRange.getRows().getByIndex(4)
oSelCol = oSelRange.getColumns().getByIndex(1)
oSelRow.setPropertyValue("IsStartOfNewPage", True) #4行目の下に改ページ設定
oSelCol.setPropertyValue("IsStartOfNewPage", True) #B列の左に改ページ設定
omsgbox('改ページ設定',1,'LO6.4.3.2(x64)')
#
oSelRow = oSelRange.getRows()
oSelCol = oSelRange.getColumns()
oSelRow.setPropertyValue("IsStartOfNewPage", False) #全ての行の改ページ削除
oSelCol.setPropertyValue("IsStartOfNewPage", False) #全ての列の改ページ削除
oDisp = '改ページ削除'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,'LO6.4.3.2(x64)')

#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
#
def omsgbox(oMessage='',oBtnType=1,oTitle='Title'):
# """shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, oBtnType, oTitle, oMessage)
return msgbox.execute()
#
def hasUnoInterfaces(obj, *interfaces):
return set(interfaces).issubset(t.typeName for t in obj.Types)
#
def oTest(*args):
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByIndex(0)
#
#Print Areaの設定
oPrintArea = uno.createUnoStruct('com.sun.star.table.CellRangeAddress')
oPrintArea.StartColumn = 0
oPrintArea.StartRow = 0
oPrintArea.EndColumn = 3
oPrintArea.EndRow = 9
# oPrintAreaのデータをList化
oDataRng = []
oDataRng.append(oPrintArea)
#
oSheet.setPrintAreas(oDataRng)
#
#Print Areaの取得
ouno = 'com.sun.star.sheet.XPrintAreas'
if hasUnoInterfaces(oSheet, ouno):
oprops = oSheet.getPrintAreas()
oDisp = 'Sheet No' + '\t' + ' = ' + str(oprops[0].Sheet)
oDisp = oDisp + '\n' +'Start Column' + '\t' + ' = ' + str(oprops[0].StartColumn)
oDisp = oDisp + '\n' +'End Column' + '\t' + ' = ' + str(oprops[0].EndColumn)
oDisp = oDisp + '\n' +'Start Row' + '\t' + ' = ' + str(oprops[0].StartRow)
oDisp = oDisp + '\n' +'End Row' + '\t' + ' = ' + str(oprops[0].EndRow)
else:
oDisp = 'This Document does not support' + '\n' + 'the XPrintAreas interface'
#
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,'LO6.4.3.2(x64)')
#!
#coding: utf-8
# python Marco
from com.sun.star.beans import PropertyValue
import uno
import sys
import traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
#
def omsgbox(oMessage='',oBtnType=1,oTitle='Title'):
# """shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, oBtnType, oTitle, oMessage)
return msgbox.execute()
#
def oTest(*args):
try:
ctx = XSCRIPTCONTEXT.getComponentContext()
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
dispatchHelper = ctx.ServiceManager.createInstanceWithContext( 'com.sun.star.frame.DispatchHelper', ctx )
oProp = PropertyValue()
#印刷範囲定義
oProp.Name = 'ToPoint'
oProp.Value = 'B2:D8'
properties = (oProp,)
dispatchHelper.executeDispatch(frame, '.uno:GoToCell', '', 0, properties)
dispatchHelper.executeDispatch(frame, '.uno:DefinePrintArea', '', 0, properties)
omsgbox('印刷範囲定義',1,'LO6.4.3.2(x64)')
#印刷範囲変更
oProp.Name = 'PrintArea'
oProp.Value = 'A1:B5'
properties = (oProp,)
dispatchHelper.executeDispatch(frame, '.uno:ChangePrintArea', '', 0, properties)
omsgbox('印刷範囲変更',1,'LO6.4.3.2(x64)')
#印刷範囲追加
oProp.Name = 'ToPoint'
oProp.Value = 'A9:B10'
properties = (oProp,)
dispatchHelper.executeDispatch(frame, '.uno:GoToCell', '', 0, properties)
dispatchHelper.executeDispatch(frame, '.uno:AddPrintArea', '', 0, properties)
omsgbox('印刷範囲追加',1,'LO6.4.3.2(x64)')
#印刷範囲削除
dispatchHelper.executeDispatch(frame, '.uno:DeletePrintArea', '', 0, properties)
omsgbox('印刷範囲削除',1,'LO6.4.3.2(x64)')
#
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,'LO6.4.3.2(x64)')



#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
def omsgbox(oMessage='',oBtnType=1,oTitle='Title'):
# """shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, MESSAGEBOX, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oCtrl = oDoc.getCurrentController()
oSheet = oCtrl.getActiveSheet()
#現在のシートスタイル名取得
oPageStyleName = oSheet.PageStyle
#PageStyle設定
oPageStyle = oDoc.getStyleFamilies()['PageStyles'][oPageStyleName]
oPageStyle.Height = 297*100 # unit: 1/100mm
oPageStyle.TopMargin = 10*100 # unit: 1/100mm
oPageStyle.setPropertyValue('BottomMargin', 6*100) # setPropertyValueを使う方法
oPageStyle.HeaderIsOn = True
oPageStyle.HeaderHeight = 5*100
oPageStyle.setPropertyValue('FooterIsOn', True)
oPageStyle.setPropertyValue('FooterHeight', 7*100)
#
#PageStyle取得
oProp = 'Height', 'Width', 'TopMargin', 'BottomMargin', 'LeftMargin', 'RightMargin', 'HeaderIsOn', 'HeaderHeight', 'FooterIsOn', 'FooterHeight'
oHeight, oWidth, oTopmargin, oBottommargin, oLeftmargin, oRightmargin, oHeaderison, oHeaderheight, oFooterison, oFooterheight = oPageStyle.getPropertyValues(oProp)
#
oDisp = u'[PageStyle値]\n'\
u'PageSize 高さ: ' + str(int(oHeight/100)) + u'mm / 幅: ' + str(int(oWidth/100)) + 'mm\n'\
u'余白 Top: ' + str(int(oTopmargin/100)) + u'mm / Bottom: ' + str(int(oBottommargin/100)) + 'mm\n'\
u'余白 Left: ' + str(int(oLeftmargin/100)) + u'mm / Right: ' + str(int(oRightmargin/100)) + 'mm\n'\
u'ヘッダーON: ' + str(oHeaderison) + u' / ヘッダー高さ: ' + str(int(oHeaderheight/100)) + 'mm\n'\
u'フッターON: ' + str(oFooterison) + u' / フッター高さ: ' + str(int(oFooterheight/100)) + 'mm\n'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,'LO6.4.3.2(x64)')