「名前付き範囲」を設定するのをやめた(アドレスべた書きへ変更)。
[nucalgen] / src / main / java / jp / satomichan / nucalgen / Nucalgen.java
1 package jp.satomichan.nucalgen;
2
3 import java.io.FileInputStream;
4 import java.io.FileOutputStream;
5 import java.util.Arrays;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.apache.commons.cli.CommandLine;
11 import org.apache.commons.cli.CommandLineParser;
12 import org.apache.commons.cli.DefaultParser;
13 import org.apache.commons.cli.Option;
14 import org.apache.commons.cli.Options;
15 import org.apache.commons.configuration.XMLConfiguration;
16 import org.apache.poi.ss.usermodel.Cell;
17 import org.apache.poi.ss.usermodel.Row;
18 import org.apache.poi.ss.usermodel.Sheet;
19 import org.apache.poi.ss.usermodel.Workbook;
20 import org.apache.poi.ss.usermodel.WorkbookFactory;
21 import org.apache.poi.ss.util.CellRangeAddress;
22 import org.apache.poi.ss.util.CellReference;
23
24 public class Nucalgen {
25
26         public static void main(String[] args) {
27                 //コマンドライン・オプション読み込み
28                 Options options = new Options();
29                 options.addOption(Option.builder("s").required().hasArg().longOpt("std-food-comp-table").build());
30                 options.addOption(Option.builder("c").required().hasArg().longOpt("columns").build());
31                 options.addOption(Option.builder("o").required().hasArg().longOpt("output").build());
32                 options.addOption(Option.builder("l").required().hasArg().longOpt("lines").build());
33                 options.addOption(Option.builder("p").longOpt("use-processed-table").build());
34                 options.addOption(Option.builder("pfc").longOpt("with-pfc-balance").build());
35                 options.addOption(Option.builder("groupsum").longOpt("with-group-sum").build());
36                 options.addOption(Option.builder("bright").hasArg().longOpt("bright-colored-vegetables-list").build());
37                 options.addOption(Option.builder("protect").longOpt("set-protect").build());
38                 options.addOption(Option.builder("r").longOpt("-use-processed-table").build());
39
40                 Map<String, String> namedAreaMap = new HashMap<String, String>();
41
42                 try {
43
44                         CommandLineParser parser = new DefaultParser();
45                         CommandLine cmd = parser.parse(options, args);
46
47                         final String mextStdFoodCompTableFileName = cmd.getOptionValue("std-food-comp-table");
48                         final String columnsXmlFileName = cmd.getOptionValue("columns");
49                         final String outputXlsxFileName = cmd.getOptionValue("output");
50                         final int lines = Integer.parseInt(cmd.getOptionValue("lines"));
51
52                         //コンフィグ読み込み
53                         XMLConfiguration config = new XMLConfiguration(columnsXmlFileName);
54                         NutritionColumnHolder nc = new NutritionColumnHolder(config);
55
56                         //Book生成
57                         Workbook outputWorkbook = WorkbookFactory.create(new FileInputStream(mextStdFoodCompTableFileName));
58
59                         if(cmd.hasOption("use-processed-table") == false) {
60                                 //成分表 変換
61                                 MextStdFoodCompTable stdCompTable = new MextStdFoodCompTable(cmd.getOptionValue("bright-colored-vegetables-list"));
62                                 stdCompTable.processInto(outputWorkbook);
63                         }
64
65
66                         //「栄養価計算」シート生成
67                         Sheet calcSheet = outputWorkbook.createSheet("栄養価計算");
68                         outputWorkbook.setSheetOrder("栄養価計算", 0);
69                         if(cmd.hasOption("set-protect")) {
70                                 calcSheet.protectSheet("");
71                         }
72                         calcSheet.setColumnWidth(2, 10240);
73                         calcSheet.addMergedRegion(new CellRangeAddress(1, 2, 1, 1));
74
75                         CellStylePool csPool = new CellStylePool(outputWorkbook);
76
77                         //「タイトル」行
78                         Row titleRow = calcSheet.createRow(1);
79                         titleRow.createCell(1).setCellValue("食品番号");
80                         titleRow.createCell(2).setCellValue("食品名");
81                         titleRow.createCell(3).setCellValue("摂取量");
82                         int colIndex = 4;
83                         for(NutritionColumn aColumn : nc.getNutritionColumnList()) {
84                                 titleRow.createCell(colIndex).setCellValue(aColumn.getDispName());
85                                 colIndex++;
86                         }
87
88                         //「単位」行
89                         Row unitRow = calcSheet.createRow(2);
90                         unitRow.createCell(2).setCellValue("単位");
91                         unitRow.createCell(3).setCellValue("g");
92                         colIndex = 4;
93                         for(NutritionColumn aColumn : nc.getNutritionColumnList()) {
94                                 unitRow.createCell(colIndex).setCellValue(aColumn.getUnit());
95                                 colIndex++;
96                         }
97
98                         //「栄養計算」行
99                         int rowIndex = 3;
100                         for(int i = rowIndex; i < lines + 3; i++,rowIndex++) {
101                                 Row thisRow = calcSheet.createRow(rowIndex);
102
103                                 thisRow.createCell(1).setCellStyle(csPool.getCellStyle("00000", false));
104                                 thisRow.createCell(2).setCellFormula("IFERROR(VLOOKUP(B" + (rowIndex + 1) + ",成分表!$B$12:$BL$2500,3,FALSE),\"\")");
105                                 thisRow.createCell(3).setCellStyle(csPool.getCellStyle("", false));
106
107                                 colIndex = 4;
108                                 for(NutritionColumn aColumn : nc.getNutritionColumnList()) {
109                                         Cell thisCell = thisRow.createCell(colIndex);
110                                         thisCell.setCellStyle(csPool.getCellStyle(aColumn.getFormat()));
111
112                                         String div100 = aColumn.isUseRawValue() ? "" :  "/ 100 * $D" + (rowIndex + 1);
113
114                                         thisCell.setCellFormula("IFERROR(VLOOKUP($B" + (rowIndex + 1) + "," + aColumn.getTable() + "!$B$12:$BL$2500,MATCH(\"" + aColumn.getName() + "\"," + aColumn.getTable() + "!$B$11:$BL$11,0),FALSE) " + div100 + ",\"\")");
115                                         colIndex++;
116                                 }
117
118                         }
119
120
121                         //摂取量 名前付き範囲
122                         String intakeArea = new CellReference(3, 3, true, true).formatAsString() + ":" + new CellReference(rowIndex -1, 3, true, true).formatAsString();
123                         //XSSFName intakeNamedRangeArea = (XSSFName) outputWorkbook.createName();
124                         //intakeNamedRangeArea.setNameName("AREA_INTAKE");
125                         //intakeNamedRangeArea.setRefersToFormula(intakeArea);
126                         namedAreaMap.put("AREA_INTAKE", intakeArea);
127
128
129                         //「合計」行
130                         Row sumRow = calcSheet.createRow(rowIndex);
131                         sumRow.createCell(1).setCellValue("合計");
132                         calcSheet.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 1, 3));
133                         colIndex = 4;
134                         for(NutritionColumn aColumn : nc.getNutritionColumnList()) {
135                                 Cell thisCell = sumRow.createCell(colIndex);
136                                 String sumTargetArea = new CellReference(3, colIndex, true, true).formatAsString() + ":" + new CellReference(rowIndex -1, colIndex, true, true).formatAsString();
137
138                                 //名前付き範囲(alias あれば設定)
139                                 if(aColumn.getAlias().length() > 0) {
140                                         //XSSFName namedRangeArea = (XSSFName) outputWorkbook.createName();
141                                         //namedRangeArea.setNameName("AREA_" + aColumn.getAlias());
142                                         //namedRangeArea.setRefersToFormula(sumTargetArea);
143                                         namedAreaMap.put("AREA_" + aColumn.getAlias(), sumTargetArea);
144
145                                         if(aColumn.isUseSum()) {
146                                                 //XSSFName namedRangeSum = (XSSFName) outputWorkbook.createName();
147                                                 //namedRangeSum.setNameName("SUM_" + aColumn.getAlias());
148                                                 String sumArea = new CellReference(rowIndex, colIndex, true, true).formatAsString();
149                                                 //namedRangeSum.setRefersToFormula(sumArea);
150                                                 namedAreaMap.put("SUM_" + aColumn.getAlias(), sumArea);
151                                         }
152                                 }
153
154                                 thisCell.setCellStyle(csPool.getCellStyle(aColumn.getFormat()));
155                                 if(aColumn.isUseSum()) {
156                                         thisCell.setCellFormula("SUM(" + sumTargetArea + ")");
157                                 }
158                                 colIndex++;
159                         }
160
161
162                         //「PFCバランス」出力
163                         if(cmd.hasOption("with-pfc-balance")) {
164                                 rowIndex += 3;
165                                 rowIndex = generatePfcBalance(calcSheet, csPool, rowIndex, namedAreaMap);
166                         }
167
168                         //「食品群別摂取量」出力
169                         if(cmd.hasOption("with-group-sum")) {
170                                 rowIndex += 3;
171                                 rowIndex = generateGroupSum(calcSheet, csPool, rowIndex, namedAreaMap);
172                         }
173
174                         //ブック出力
175                         FileOutputStream outputXlsxFile = new FileOutputStream(outputXlsxFileName);
176                         outputWorkbook.setActiveSheet(0);
177                         calcSheet.setForceFormulaRecalculation(true);
178                         outputWorkbook.setSelectedTab(0);
179                         outputWorkbook.write(outputXlsxFile);
180                         outputWorkbook.close();
181
182                 } catch (Exception e) {
183                         // TODO 自動生成された catch ブロック
184                         e.printStackTrace();
185                 }
186         }
187
188
189
190         //PFCバランス
191         private static int generatePfcBalance(Sheet calcSheet, CellStylePool csPool, int rowIndex, Map<String,String> _namedAreaMap) {
192                 Row pfbBalanceRow1 = calcSheet.createRow(rowIndex);
193                 pfbBalanceRow1.createCell(1).setCellValue("PFCバランス (%)");
194                 calcSheet.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 1, 2));
195                 pfbBalanceRow1.createCell(3).setCellValue("P");
196                 pfbBalanceRow1.createCell(4).setCellValue("F");
197                 pfbBalanceRow1.createCell(5).setCellValue("C");
198
199                 final String sumPfc = "(" + _namedAreaMap.get("SUM_P") + "*4+" + _namedAreaMap.get("SUM_F") + "*9+" + _namedAreaMap.get("SUM_C") + "*4)";
200
201                 rowIndex++;
202                 Row pfbBalanceRow2 = calcSheet.createRow(rowIndex);
203                 Cell pCell = pfbBalanceRow2.createCell(3);
204                 pCell.setCellStyle(csPool.getCellStyle("0"));
205                 pCell.setCellFormula(_namedAreaMap.get("SUM_P") + "*4*100/" + sumPfc);
206
207                 Cell fCell = pfbBalanceRow2.createCell(4);
208                 fCell.setCellStyle(csPool.getCellStyle("0"));
209                 fCell.setCellFormula(_namedAreaMap.get("SUM_F") + "*9*100/" + sumPfc);
210                 Cell cCell = pfbBalanceRow2.createCell(5);
211                 cCell.setCellStyle(csPool.getCellStyle("0"));
212                 cCell.setCellFormula(_namedAreaMap.get("SUM_C") + "*4*100/" + sumPfc);
213
214                 return rowIndex;
215         }
216
217
218
219          //群別摂取量
220         private static int generateGroupSum(Sheet calcSheet, CellStylePool csPool, int rowIndex, Map<String,String> _namedAreaMap) {
221
222                 List<String> groupName = Arrays.asList("0", "穀類", "いも及びでん粉類", "砂糖及び甘味類", "豆類",
223                                 "種実類", "野菜類", "果実類", "きのこ類", "藻類", "魚介類", "肉類", "卵類", "乳類",
224                                 "油脂類", "菓子類", "し好飲料類", "調味料及び香辛料類", "調理加工食品類");
225
226
227
228                 Row groupRow = calcSheet.createRow(rowIndex);
229                 groupRow.createCell(1).setCellValue("食品群");
230                 groupRow.createCell(3).setCellValue("摂取量(g)");
231                 rowIndex++;
232
233                 for(int i = 1; i <= 18; i++,rowIndex++) {
234                         Row thisRow = calcSheet.createRow(rowIndex);
235                         thisRow.createCell(1).setCellValue(i);
236                         thisRow.createCell(2).setCellValue(groupName.get(i));
237                         Cell cCell = thisRow.createCell(3);
238                         cCell.setCellStyle(csPool.getCellStyle(""));
239                         //cCell.setCellFormula("SUMIF(AREA_GROUP, " + i + ", AREA_INTAKE)");
240                         cCell.setCellFormula("SUMIF(" + _namedAreaMap.get("AREA_GROUP") + ", " + i + ", " + _namedAreaMap.get("AREA_INTAKE") + ")");
241
242                         if(i == 6) {
243                                 rowIndex++;
244                                 thisRow = calcSheet.createRow(rowIndex);
245                                 thisRow.createCell(2).setCellValue("うち 緑黄色野菜");
246                                 Cell bcvCell = thisRow.createCell(3);
247                                 bcvCell.setCellStyle(csPool.getCellStyle("0"));
248                                 //bcvCell.setCellFormula("SUMIF(AREA_BRIGHT_COLORED_VEGETABLE, 1, AREA_INTAKE)");
249                                 bcvCell.setCellFormula("SUMIF(" + _namedAreaMap.get("AREA_BRIGHT_COLORED_VEGETABLE") + ", 1, " + _namedAreaMap.get("AREA_INTAKE") + ")");
250                         }
251
252
253                 }
254
255                 return rowIndex;
256         }
257
258
259
260
261
262
263
264
265
266
267 }