Cell操作
[ Basic ]
[ Insert・Delete.Copy ]
[ Property(Cellの書式設定) ]
{{ Format }}
{{ Font }}
[ Address ]
[ HyperLink ]
Sheet
File
Cell操作
[ Basic ]
# -*- 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()
oDoc = desktop.loadComponentFromURL( oURL,"_blank", 0, () )
#
oSheet = oDoc.getSheets().getByIndex(0)
oCell = oSheet.getCellByPosition(0,0)
#
oCell.String = 'MacroTest'
except:
pass
# -*- 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()
oDoc = desktop.loadComponentFromURL( oURL,"_blank", 0, () )
#
oSheet = oDoc.getSheets().getByIndex(0)
#
oSheet.getCellByPosition(0,0).Value = 1
oSheet.getCellByPosition(0,1).Value = 2
oSheet.getCellByPosition(0,2).Formula = "=sum(A1:A2)"
except:
pass
#!
#coding: utf-8
# python Marco
import uno
import unohelper
import sys
import traceback
from com.sun.star.awt import Rectangle
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
desktop = XSCRIPTCONTEXT.getDesktop()
oDoc = desktop.loadComponentFromURL('private:factory/scalc','_blank', 0, () )
#
oSheet = oDoc.getSheets().getByName('sheet1')
#
oSheet.getCellRangeByName('A1').Value = 1
oSheet.getCellRangeByName('A2').Value = 2
oSheet.getCellRangeByName('A3').Formula = '=sum(A1:A2)'
oSheet.getCellRangeByName('B1').String = 'LibreOffice'
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
# -*- coding: utf-8 -*-
import uno
import unohelper
import os.path
from com.sun.star.awt import Rectangle
def show_message(desktop, message):
"""shows message."""
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(
window, Rectangle(), 'messbox', 1, '', message)
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)
#
oSheet.getCellByPosition(0,0).Value = 3
oSheet.getCellByPosition(0,1).Value = 2
oSheet.getCellByPosition(0,2).Formula = "=sum(A1:A2)"
#
oVal = oSheet.getCellByPosition(0,2).Value
oDisp = oVal
#
show_message(desktop, oDisp)
except:
pass
[ Insert・Delete.Copy ]
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet =oDoc.getSheets().getByName('sheet1')
#
oCellRangeAddr = uno.createUnoStruct('com.sun.star.table.CellRangeAddress')
oCellRangeAddr.Sheet = 0
oCellRangeAddr.StartColumn = 2
oCellRangeAddr.StartRow = 2
oCellRangeAddr.EndColumn = 4
oCellRangeAddr.EndRow = 4
oSheet.insertCells(oCellRangeAddr, 1) # com.sun.star.sheet.CellInsertMode.DOWN
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet =oDoc.getSheets().getByName('sheet1')
#
oCellRangeAddr = uno.createUnoStruct('com.sun.star.table.CellRangeAddress')
oCellRangeAddr.Sheet = 0
oCellRangeAddr.StartColumn = 2
oCellRangeAddr.StartRow = 2
oCellRangeAddr.EndColumn = 4
oCellRangeAddr.EndRow = 4
oSheet.insertCells(oCellRangeAddr, 2) # com.sun.star.sheet.CellInsertMode.RIGHT
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet =oDoc.getSheets().getByName('sheet1')
#
oCellRangeAddr = uno.createUnoStruct('com.sun.star.table.CellRangeAddress')
oCellRangeAddr.Sheet = 0
oCellRangeAddr.StartColumn = 2
oCellRangeAddr.StartRow = 2
oCellRangeAddr.EndColumn = 4
oCellRangeAddr.EndRow = 4
oSheet.insertCells(oCellRangeAddr, 3) # com.sun.star.sheet.CellInsertMode.ROW
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet =oDoc.getSheets().getByName('sheet1')
#
oCellRangeAddr = uno.createUnoStruct('com.sun.star.table.CellRangeAddress')
oCellRangeAddr.Sheet = 0
oCellRangeAddr.StartColumn = 2
oCellRangeAddr.StartRow = 2
oCellRangeAddr.EndColumn = 4
oCellRangeAddr.EndRow = 4
oSheet.insertCells(oCellRangeAddr, 4) # com.sun.star.sheet.CellInsertMode.COLUMNS
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
from com.sun.star.beans import PropertyValue
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
ctx = XSCRIPTCONTEXT.getComponentContext()
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
dispatchHelper = ctx.ServiceManager.createInstanceWithContext( 'com.sun.star.frame.DispatchHelper', ctx )
# B2へ移動
oProp = PropertyValue()
oProp.Name = 'ToPoint'
oProp.Value = 'C3:E5'
properties = (oProp,)
dispatchHelper.executeDispatch( frame, '.uno:GoToCell', '', 0, properties )
#
oProp.Name = 'Flags'
oProp.Value = 'V'
properties = (oProp,)
dispatchHelper.executeDispatch( frame, '.uno:InsertCell', '', 0, properties )
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#
# [ Note ]
# V : Cellを下に移動
# > : Cellを右に移動
# R : 行全体を下に移動
# C : 列全体を右に移動
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet =oDoc.getSheets().getByName('sheet1')
#
oCellRangeAddr = uno.createUnoStruct('com.sun.star.table.CellRangeAddress')
oCellRangeAddr.Sheet = 0
oCellRangeAddr.StartColumn = 2
oCellRangeAddr.StartRow = 2
oCellRangeAddr.EndColumn = 4
oCellRangeAddr.EndRow = 4
oSheet.removeRange(oCellRangeAddr, 1) # com.sun.star.sheet.CellDeleteMode.UP
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet =oDoc.getSheets().getByName('sheet1')
#
oCellRangeAddr = uno.createUnoStruct('com.sun.star.table.CellRangeAddress')
oCellRangeAddr.Sheet = 0
oCellRangeAddr.StartColumn = 2
oCellRangeAddr.StartRow = 2
oCellRangeAddr.EndColumn = 4
oCellRangeAddr.EndRow = 4
oSheet.removeRange(oCellRangeAddr, 2) # com.sun.star.sheet.CellDeleteMode.LEFT
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet =oDoc.getSheets().getByName('sheet1')
#
oCellRangeAddr = uno.createUnoStruct('com.sun.star.table.CellRangeAddress')
oCellRangeAddr.Sheet = 0
oCellRangeAddr.StartColumn = 2
oCellRangeAddr.StartRow = 2
oCellRangeAddr.EndColumn = 4
oCellRangeAddr.EndRow = 4
oSheet.removeRange(oCellRangeAddr, 3) # com.sun.star.sheet.CellDeleteMode.ROWS
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet =oDoc.getSheets().getByName('sheet1')
#
oCellRangeAddr = uno.createUnoStruct('com.sun.star.table.CellRangeAddress')
oCellRangeAddr.Sheet = 0
oCellRangeAddr.StartColumn = 2
oCellRangeAddr.StartRow = 2
oCellRangeAddr.EndColumn = 4
oCellRangeAddr.EndRow = 4
oSheet.removeRange(oCellRangeAddr, 4) # com.sun.star.sheet.CellDeleteMode.COLUMNS
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
from com.sun.star.beans import PropertyValue
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
ctx = XSCRIPTCONTEXT.getComponentContext()
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
dispatchHelper = ctx.ServiceManager.createInstanceWithContext( 'com.sun.star.frame.DispatchHelper', ctx )
# B2へ移動
oProp = PropertyValue()
oProp.Name = 'ToPoint'
oProp.Value = 'C3:E5'
properties = (oProp,)
dispatchHelper.executeDispatch( frame, '.uno:GoToCell', '', 0, properties )
#
oProp.Name = 'Flags'
oProp.Value = 'U'
properties = (oProp,)
dispatchHelper.executeDispatch( frame, '.uno:DeleteCell', '', 0, properties )
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#
# [ Flag Value ]
# U : Cell を 上に移動
# L : Cell を 左に移動
# R : 行全体を削除
# C : 列全体を削除
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet =oDoc.getSheets().getByIndex(0)
#
oRangeAddress = oSheet.getCellRangeByName('B2:D4').getRangeAddress()
oCellAddress = oSheet.getCellByPosition(0,10).getCellAddress()
oSheet.copyRange(oCellAddress, oRangeAddress)
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet1 = oDoc.getSheets().getByIndex(0)
oSheet2 = oDoc.getSheets().getByIndex(1)
sCol = 0
eCol = 3
sRow = 0
eRow = 10
oCopyRange = oSheet1.getCellRangeByPosition(sCol, sRow, eCol, eRow)
oPasteRange = oSheet2.getCellRangeByPosition(sCol, sRow, eCol, eRow)
oCopyData = oCopyRange.getData()
oPasteRange.setData(oCopyData)
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet1 = oDoc.getSheets().getByIndex(0)
oSheet2 = oDoc.getSheets().getByIndex(1)
sCol = 0
eCol = 3
sRow = 0
eRow = 5
oCopyRange = oSheet1.getCellRangeByPosition(sCol, sRow, eCol, eRow)
oPasteRange = oSheet2.getCellRangeByPosition(sCol+1, sRow+1, eCol+1, eRow+1)
oCopyData = oCopyRange.getDataArray()
oPasteRange.setDataArray(oCopyData)
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
from com.sun.star.beans import PropertyValue
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
ctx = XSCRIPTCONTEXT.getComponentContext()
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
dispatchHelper = ctx.ServiceManager.createInstanceWithContext( 'com.sun.star.frame.DispatchHelper', ctx )
# Move A1 cell and Copy
oProp = PropertyValue()
oProp.Name = 'ToPoint'
oProp.Value = 'A1'
properties = (oProp,)
dispatchHelper.executeDispatch( frame, '.uno:GoToCell', '', 0, properties )
properties = ()
dispatchHelper.executeDispatch( frame, '.uno:Copy', '', 0, properties )
#
oProp = PropertyValue()
oProp.Name = 'ToPoint'
oProp.Value = 'B2'
properties = (oProp,)
dispatchHelper.executeDispatch( frame, '.uno:GoToCell', '', 0, properties )
# Initialize
oProp = []
oProp1 = PropertyValue()
oProp1.Name = 'Flags'
oProp1.Value = 'SDFNT'
oProp2 = PropertyValue()
oProp2.Name = 'FormulaCommand'
oProp2.Value = 0
oProp3 = PropertyValue()
oProp3.Name = 'SkipEmptyCells'
oProp3.Value = False # false : NG
oProp4 = PropertyValue()
oProp4.Name = 'Transpose'
oProp4.Value = False
oProp5 = PropertyValue()
oProp5.Name = 'AsLink'
oProp5.Value = False
oProp6 = PropertyValue()
oProp6.Name = 'MoveMode'
oProp6.Value = 4
oProp.append(oProp1)
oProp.append(oProp2)
oProp.append(oProp3)
oProp.append(oProp4)
oProp.append(oProp5)
oProp.append(oProp6)
properties = tuple(oProp)
dispatchHelper.executeDispatch( frame, '.uno:InsertContents', '', 0, properties )
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#
# [ Flag Value ]
# S : String ( テキスト )
# V : Value ( 値 )
# D : Date ( 日付 )
# F : Formula ( 式 )
# N : Note ( コメント )
# T : Format ( 書式 )
# 空白 : Object ( オブジェクト )
# A : 全て
[ Property(Cellの書式設定) ]
{{ Format }}
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
from com.sun.star.beans import PropertyValue
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
#
oLocale = uno.createUnoStruct("com.sun.star.lang.Locale")
oLocale.Language = "ja"
oLocale.Country = "JP"
oNumberFormats = oDoc.NumberFormats
oList1 = ['#,##0','#,##0.#0','0%','0.00%','[$¥-411]#,##0;-[$¥-411]#,##0','[$¥-411]#,##0;[RED]-[$¥-411]#,##0']
oList2 = ['YYYY/MM/DD',u'YYYY年MM月DD日(AAAA)','GE.M.D','HH:MM','HH:MM:SS','0.00E+00','# ??/??']
oDF = oList1 + oList2
oDisp = u'[ 表示Key No ]'
for n in oDF:
oKeyNo = oNumberFormats.queryKey( n, oLocale, False ) # false : NG
oDisp = oDisp + '\n' + n + ' ⇒ ' + str(oKeyNo)
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByName('sheet1')
oCell=oSheet.getCellRangeByName('A1')
#
oLocale = uno.createUnoStruct('com.sun.star.lang.Locale')
oLocale.Language = 'ja'
oLocale.Country = 'JP'
NumberFormats = oDoc.NumberFormats
NumberFormatString = u'#,##0.#0円'
NumberFormatId = NumberFormats.queryKey(NumberFormatString, oLocale, True)
if NumberFormatId == -1:
NumberFormatId = NumberFormats.addNew(NumberFormatString, oLocale) # 書式コードを追加
oCell.NumberFormat = NumberFormatId
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByName('sheet1')
oCell=oSheet.getCellRangeByName('A1')
oCell.Value = 20000 # value : NG
oCell.NumberFormat = 10103
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
from com.sun.star.beans import PropertyValue
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
ctx = XSCRIPTCONTEXT.getComponentContext()
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
dispatchHelper = ctx.ServiceManager.createInstanceWithContext( 'com.sun.star.frame.DispatchHelper', ctx )
# B2へ移動
oProp = PropertyValue()
oProp.Name = 'ToPoint'
oProp.Value = 'A1'
properties = (oProp,)
dispatchHelper.executeDispatch( frame, '.uno:GoToCell', '', 0, properties )
#
oProp.Name = 'NumberFormatValue'
oProp.Value = 25
properties = (oProp,)
dispatchHelper.executeDispatch( frame, '.uno:NumberFormatValue', '', 0, properties )
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByName('sheet1')
oCell=oSheet.getCellRangeByName('A1')
oCell.String = 'LibreOffice' # string : NG
oType = oCell.getType()
#
if oType == uno.Enum('com.sun.star.table.CellContentType', 'EMPTY'):
oDisp = 'Empty'
elif oType == uno.Enum('com.sun.star.table.CellContentType', 'VALUE'):
oDisp = 'Value'
elif oType == uno.Enum('com.sun.star.table.CellContentType', 'TEXT'):
oDisp = 'Text'
elif oType == uno.Enum('com.sun.star.table.CellContentType', 'FORMULA'):
oDisp = 'Formula'
else:
oDisp = 'UnKnown'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
from com.sun.star.beans import PropertyValue
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByName('sheet1')
oCell=oSheet.getCellRangeByName('A1')
#
oUserAttr = uno.createUnoStruct('com.sun.star.xml.AttributeData')
# xray oUserAtrr
oUserAttr.Type ="CDATA"
oUserAttr.Value = "NewOOo3 macro"
oUserData = oCell.UserDefinedAttributes
#
if not oUserData.hasByName('home'):
oUserData.insertByName("home",oUserAttr)
oCell.UserDefinedAttributes = oUserData
# xray oUserData
oUser = oUserData.ElementNames
#
oDisp = '[ User Define Attributes ]'
for n in oUser:
oDisp = oDisp + '\n' + n
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
#
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
#
def ColumnNumberToString(nColumn):
oRtn = ''
while nColumn >= 0:
oAsc = 65 + (nColumn%26)
oReturn2= chr(oAsc)
oRtn = str(oReturn2) + oRtn
nColumn= int(nColumn / 26 - 1)
else:
return str(oRtn)
#
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByName('sheet1')
oGetFormat = oSheet.getCellFormatRanges()
oDisp = '[ getCellFormatRanges() ]'
for i in range(oGetFormat.getCount()):
oSheetCellRange = oGetFormat.getByIndex(i)
oAddress = oSheetCellRange.getRangeAddress()
oDisp = oDisp + '\n\t\t' + str(i) + ' = Sheet' + str(oAddress.Sheet +1) + '.'
oDisp = oDisp + ColumnNumberToString(oAddress.StartColumn) + str(oAddress.StartRow + 1) + ':'
oDisp = oDisp + ColumnNumberToString(oAddress.EndColumn) + str(oAddress.EndRow + 1) + '\n'
#
oGetFormat = oSheet.getUniqueCellFormatRanges()
oDisp = oDisp + '\n[ getUniqueCellFormatRanges() ]'
for i in range(oGetFormat.getCount()):
oSheetUniqueRange = oGetFormat.getByIndex(i)
oDisp = oDisp + '\n\t\t' + str(i) + ' = ' + str(oSheetUniqueRange.getRangeAddressesAsString())
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp)
{{ Font }}
#!
#coding: utf-8
# python Marco
import uno
import sys
import traceback
from com.sun.star.awt import Rectangle
class cFontSlant():
from com.sun.star.awt.FontSlant import (NONE, ITALIC, REVERSE_ITALIC,OBLIQUE,)
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, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
def oMacroTest():
try:
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getSheets().getByIndex(0)
oCell = oSheet.getCellByPosition(0,1)
oCell.String = '水 素はH2'
oCell.CharFontName = "Arial"
oCell.CharFontNameAsian = "Arial"
oCell.CharPosture = cFontSlant.ITALIC
oCell.CharPostureAsian = cFontSlant.OBLIQUE
oCell.CharHeight=40 # 英数字サイズは40(Cell単位での設定)
oCell.CharHeightAsian=20 # 日本語は20(Cell単位での設定)
oTextCursor = oCell.createTextCursor()
oTextCursor.gotoStart(False) # false : NG
oTextCursor.goRight(3, True)
oTextCursor.setPropertyValue('CharContoured', True ) #中抜き効果
oTextCursor.setPropertyValue('CharCrossedOut', True ) #取り消し線
oTextCursor.setPropertyValue('CharEmphasis',3) #強調文字 3は「・」の上付き、4は「、」の上付
oTextCursor.setPropertyValue('CharUnderlineColor', 2918503 ) #下線色 / 白抜きにしているので無意味
oTextCursor.setPropertyValue('CharUnderline',1) #UnderLine
oTextCursor.setPropertyValue('CharRelief',1) #浮き出し 0はNormal 1は浮き出し効果
oTextCursor.setPropertyValue('CharShadowed',True) #Shadow効果
oTextCursor.gotoEnd(False)
oDisp = 'Success'
except Exception as er:
oDisp = ''
oDisp = str(traceback.format_exc()) + '\n' + str(er)
finally:
omsgbox(oDisp,1,u'Python/Font')
# -*- coding: utf-8 -*-
import uno
import unohelper
import os.path
from com.sun.star.awt import Rectangle
def omsgbox(message):
# """shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, Rectangle(), 'messbox', 1, 'Title', message)
return msgbox.execute()
from com.sun.star.table import ShadowFormat
def oMacroTest():
try:
ofile= os.path.abspath('c:/temp/MacroCalc.ods')
if os.path.isfile(ofile): # If is Bad.
oURL = unohelper.systemPathToFileUrl(ofile)
#
desktop = XSCRIPTCONTEXT.getDesktop()
oDoc = desktop.loadComponentFromURL( oURL,"_blank", 0, () )
#
oSheet = oDoc.getSheets().getByIndex(0)
#
oCell = oSheet.getCellByPosition(0,0)
#
oCell.CellBackColor = 0xff0000 # 背景色=> RGBの16進数6桁で表示 0x + RGB(255,0,0)=> 0xff0000
else:
oDisp = str( ofile + ' => does not Exist!!')
omsgbox(oDisp)
except:
pass
# -*- coding: utf-8 -*-
import uno
import unohelper
import os.path
from com.sun.star.awt import Rectangle
def omsgbox(message):
# """shows message."""
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, Rectangle(), 'messbox', 1, 'Title', message)
return msgbox.execute()
from com.sun.star.table import ShadowFormat
def oMacroTest():
try:
ofile= os.path.abspath('c:/temp/MacroCalc.ods')
if os.path.isfile(ofile): # If is Bad.
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 = 'MacroTest'
oCell.CharColor = 0xff0000 # 文字色=> RGBの16進数6桁で表示 0x + RGB(255,0,0)=> 0xff0000
oCell.CharHeight = 15 # 文字Size
oCell.CharWeight = 100 # 文字太さ
else:
oDisp = str( ofile + ' => does not Exist!!')
omsgbox(oDisp)
except:
pass
[ Address ]
import uno
from com.sun.star.awt import Rectangle
def show_message(desktop, message):
"""shows message."""
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(
window, Rectangle(), 'messbox', 1, '', message)
return msgbox.execute()
def get_active_cell():
doc = XSCRIPTCONTEXT.getDocument()
selected = doc.getCurrentSelection()
try:
if selected.supportsService('com.sun.star.sheet.SheetCellRange'):
addr = selected.getRangeAddress()
txt = 'Column: %s\nRow: %s' % (addr.EndColumn, addr.EndRow)
show_message(XSCRIPTCONTEXT.getDesktop(), txt)
except:
pass
import uno
from com.sun.star.awt import Rectangle
def show_message(desktop, message):
"""shows message."""
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(
window, Rectangle(), 'messbox', 1, '', message)
return msgbox.execute()
from com.sun.star.sheet.CellFlags import (VALUE, DATETIME, STRING,
ANNOTATION, FORMULA, HARDATTR, OBJECTS, EDITATTR, FORMATTED)
def get_used_last_row_index():
"""shows last used row index of specified column"""
column_index = 0
sheet_index = 0
flags = VALUE + DATETIME + STRING + \
ANNOTATION + FORMULA + HARDATTR + OBJECTS + EDITATTR + FORMATTED
doc = XSCRIPTCONTEXT.getDocument()
sheet = doc.getSheets().getByIndex(sheet_index)
column = sheet.getColumns().getByIndex(column_index)
ranges = column.queryContentCells(flags).getRangeAddresses()
n = 0
for r in ranges:
if n < r.EndRow:
n = r.EndRow
show_message(XSCRIPTCONTEXT.getDesktop(), 'last row: %s' % n)
[ HyperLink ]
def create_url_field():
"""make url field."""
url = 'http://openoffice3.web.fc2.com/'
text = 'Link!'
doc = XSCRIPTCONTEXT.getDocument()
# new URL field
field = doc.createInstance('com.sun.star.text.textfield.URL')
field.URL = url
field.Representation = text
# insert it
cell = doc.getSheets().getCellByPosition(0, 0, 0)
cell.insertTextContent(cell.createTextCursor(), field, False)
Sheet
# -*- coding: utf-8 -*-
import uno
import unohelper
import os.path
from com.sun.star.awt import Rectangle
def omsgbox(desktop,message):
# """shows message."""
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, Rectangle(), 'messbox', 1, 'Title', message)
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 import Rectangle
def omsgbox(desktop,message):
# """shows message."""
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, Rectangle(), 'messbox', 1, 'Title', message)
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
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 -*-
import uno
import unohelper
import os.path
from com.sun.star.awt import Rectangle
from com.sun.star.beans import PropertyValue
def omsgbox(desktop,message):
# """shows message."""
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, Rectangle(), 'messbox', 1, 'Title', message)
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