Last active
June 9, 2023 20:53
-
-
Save zaki-yama/9ce8d81182d3094c28b4 to your computer and use it in GitHub Desktop.
バッチクラスで CSV ファイルを読み込むサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| global with sharing class CSVIterator implements Iterator<String>, Iterable<String> { | |
| private String csvData; | |
| private String rowDelimiter; | |
| public CSVIterator(String fileData, String rowDelimiter) { | |
| this.csvData = fileData; | |
| this.rowDelimiter = rowDelimiter; | |
| } | |
| global Boolean hasNext() { | |
| return csvData.length() > 1 ? true : false; | |
| } | |
| global String next() { | |
| String row = this.csvData.subString(0, this.csvData.indexOf(this.rowDelimiter)); | |
| // Delimiter の直後から CSV ファイルの末尾まで = 先頭1行だけ切り取り | |
| this.csvData = this.csvData.subString( | |
| this.csvData.indexOf(this.rowDelimiter) + this.rowDelimiter.length(), | |
| this.csvData.length() | |
| ); | |
| return row; | |
| } | |
| public Iterator<String> Iterator() { | |
| return this; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CSVParser { | |
| public List<String> parse(String csvRow) { | |
| // FIXME: ダブルクォート(") で囲まれた値にも対応する | |
| return csvRow.split(','); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <apex:page controller="CSVUploadController"> | |
| <apex:sectionheader title="CSVアップロード"> | |
| <apex:form > | |
| <apex:pageblock title="ファイルの指定"> | |
| <apex:pageblocksection columns="1"> | |
| <apex:pageblocksectionitem > | |
| <apex:inputfile value="{!file}"> | |
| </apex:inputfile></apex:pageblocksectionitem> | |
| </apex:pageblocksection> | |
| <apex:pageblockbuttons location="bottom"> | |
| <apex:commandbutton action="{!readFile}" value="アップロード"> | |
| </apex:commandbutton></apex:pageblockbuttons> | |
| </apex:pageblock> | |
| </apex:form> | |
| </apex:sectionheader> | |
| </apex:page> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| global with sharing class CSVUploadBatch implements Database.Batchable<String>, Database.Stateful { | |
| private String csvFile; | |
| private CSVParser parser = new CSVParser(); | |
| private static final String CRLF = '\n'; | |
| public CSVUploadBatch(Blob file) { | |
| this.csvFile = file.toString(); | |
| } | |
| global Iterable<String> start(Database.batchableContext batchableContext) { | |
| return new CSVIterator(csvFile, CRLF); | |
| } | |
| global void execute(Database.BatchableContext batchableContext, List<String> scope) { | |
| System.debug(LoggingLevel.INFO, 'execute'); | |
| System.debug(LoggingLevel.INFO, scope.size()); | |
| for (String csvRow : scope) { | |
| List<String> values = this.parser.parse(csvRow); | |
| // 処理 | |
| for (String value : values) { | |
| System.debug(LoggingLevel.INFO, value); | |
| } | |
| } | |
| } | |
| global void finish(Database.BatchableContext batchableContext) { | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CSVUploadController { | |
| public Blob file {get; set;} | |
| // SCOPE_SIZE = バッチの execute に一度に渡される CSV ファイルの行数 | |
| private static final Integer SCOPE_SIZE = 5; | |
| public PageReference readFile() { | |
| CSVUploadBatch batch = new CSVUploadBatch(file); | |
| Database.executeBatch(batch, SCOPE_SIZE); | |
| return null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment