回答編集履歴

1

追記(サンプルコード)

2019/09/04 04:48

投稿

退会済みユーザー
test CHANGED
@@ -49,3 +49,63 @@
49
49
  1. 31日がない日はAG列に翌月の日付が入るので、条件分岐を使うことをオススメします。
50
50
 
51
51
  2. 少ないデータなのでいいですが、```getRange(row, col).getValue()```を使うより、```getRange(row, col, numRow. numCol).setValues()```を使うほうが処理時間が短くなります。
52
+
53
+
54
+
55
+ ##サンプルコード
56
+
57
+ ```
58
+
59
+ ///実行する月の日付を返します。前月にやる場合は2行目を編集
60
+
61
+ function main(ss) {
62
+
63
+ const d = new Date();
64
+
65
+ //d.setMonth(d.getMonth() - 1); //前月にやる場合(例:8/31に9月分を作成するとき)
66
+
67
+ const m = d.getMonth();
68
+
69
+ const sheet = ss.getActiveSheet();
70
+
71
+ const lastCol = sheet.getLastColumn() - 1; //最終列
72
+
73
+
74
+
75
+ //月末まで繰り返し
76
+
77
+ //月末が31日以外の時は最終列まで削除
78
+
79
+ const arr = [];
80
+
81
+ for(var i = 1; i < 32; i++){
82
+
83
+ d.setDate(i);
84
+
85
+ if(d.getMonth() == m){
86
+
87
+ //Utilities.formatDateの第一引数はnew Date()のまま入れます。
88
+
89
+ //わざわざ、getMonthやgetDateをしなくていいメリットがあります。
90
+
91
+ arr.push(Utilities.formatDate(d, 'Asia/Tokyo', 'M月d日'));
92
+
93
+ }
94
+
95
+ //月末が31日以外の場合
96
+
97
+ else{
98
+
99
+ sheet.deleteColumns(i + 2, lastCol - i); //AG列までないとエラー
100
+
101
+ break;
102
+
103
+ }
104
+
105
+ }
106
+
107
+ sheet.getRange(1, 3, 1, arr.length).setValues([arr]); //C列から月末までまとめて挿入
108
+
109
+ }
110
+
111
+ ```