2013年2月20日水曜日

PythonでLibreOffice(OpenOffice)のマクロ


以前のブログから転載。

pythonで会社の資料用のマクロを作ったので
備忘のためにもメモ。
内容的には機密のものはないから大丈夫w
シートに入力された平成年、月と第何週という情報から
対象の一週間の日付を自動取得するものです。
1ヶ月足すとか、そういうのもっと簡単にできないかな?

# -*- coding: utf-8 -*-
import calendar
import datetime
# 対象シート
TARGET_SHEET = 1
# 平成年数
CELL_HEISEI = (2, 1)
# 月のセル
CELL_MONTH = (4, 1)
# 週のセル
CELL_WEEK = (7, 1)
# 平成と西暦の差
DIFF_HEISEI = 1988
# 対象セル
CELL_MON = (1, 4)
CELL_TUE = (1, 10)
CELL_WED = (1, 16)
CELL_THU = (1, 22)
CELL_FRI = (1, 28)
CELL_SAT = (1, 34)
CELL_SUN = (1, 40)
def calc_date():
    doc = XSCRIPTCONTEXT.getDocument()
    if doc.supportsService("com.sun.star.sheet.SpreadsheetDocument"):
       
        sheets = doc.getSheets()
        sheet = sheets.getByIndex(TARGET_SHEET)
        # 週のリストを取得
        year = sheet.getCellByPosition(CELL_HEISEI[0], CELL_HEISEI[1]).Value + DIFF_HEISEI
        month = sheet.getCellByPosition(CELL_MONTH[0], CELL_MONTH[1]).Value
        week = sheet.getCellByPosition(CELL_WEEK[0], CELL_WEEK[1]).Value - 1
        weekList = calendar.monthcalendar(year, month)[int(week)]
        # 1週間の中で先月や来月が混じったときの対応
        flgMod = False
        # 先月や来月の日が必要か判定する
        for day in weekList:
            if day == 0:
                # 0が入ってる(つまり先月、来月が混じっている)
                flgMod = True
        # 先月が必要なのか来月が必要なのか判定する
        if flgMod and weekList[0] == 0:
            # 最初が0ってことは先月が必要
            date = datetime.date(year, month, 1)
            # 先月の末日を取得
            date = date - datetime.timedelta(1)
            # 先月末の最後の週のIndex
            finalWeek = len(calendar.monthcalendar(date.year, date.month)) - 1
            # 先月の最後の週のリストを取得
            tempWeek = calendar.monthcalendar(date.year, date.month)[finalWeek]
            # 0部分の日付を先月の日付で置換
            for i in range(len(weekList)):
                if weekList[i] == 0:
                    weekList[i] = tempWeek[i]
        elif flgMod and weekList[6] == 0:
            # 最後が0ってことは来月が必要
            date = datetime.date(year, month, max(weekList))
            # 来月の最初の日付を取得
            date = date + datetime.timedelta(1)
            # 来月の最初の週のリストを取得
            tempWeek = calendar.monthcalendar(date.year, date.month)[0]
            # 0部分の日付を来月の日付で置換
            for i in range(len(weekList)):
                if weekList[i] == 0:
                    weekList[i] = tempWeek[i]
        # 月曜日
        Cell = sheet.getCellByPosition(CELL_MON[0], CELL_MON[1])
        Cell.String = weekList[0]
        # 火曜日
        Cell = sheet.getCellByPosition(CELL_TUE[0], CELL_TUE[1])
        Cell.String = weekList[1]
        # 水曜日
        Cell = sheet.getCellByPosition(CELL_WED[0], CELL_WED[1])
        Cell.String = weekList[2]
        # 木曜日
        Cell = sheet.getCellByPosition(CELL_THU[0], CELL_THU[1])
        Cell.String = weekList[3]
        # 金曜日
        Cell = sheet.getCellByPosition(CELL_FRI[0], CELL_FRI[1])
        Cell.String = weekList[4]
        # 土曜日
        Cell = sheet.getCellByPosition(CELL_SAT[0], CELL_SAT[1])
        Cell.String = weekList[5]
        # 日曜日
        Cell = sheet.getCellByPosition(CELL_SUN[0], CELL_SUN[1])
        Cell.String = weekList[6]

0 件のコメント:

コメントを投稿