2013年2月20日水曜日

PythonでLibreOffice(OpenOffice)のマクロ


以前のブログから転載。

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

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- 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 件のコメント:

コメントを投稿