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