Skip to content

Instantly share code, notes, and snippets.

@pen
Last active February 7, 2018 22:01
Show Gist options
  • Save pen/2b5e2ac7d2b17a8cca9f138252b0e275 to your computer and use it in GitHub Desktop.
Save pen/2b5e2ac7d2b17a8cca9f138252b0e275 to your computer and use it in GitHub Desktop.
Gドライブに置いたCSVファイルをシートに読み込む
function main() {
var sheet = SpreadsheetApp.getActive().getActiveSheet();
var file = DriveApp.getFilesByName("test_1.csv").next();
import_csv(sheet, file);
}
function import_csv(sheet, file, r, c, enc) {
var table = Utilities.parseCsv(file.getBlob().getDataAsString(enc || "SHIFT_JIS");
sheet.getRange(r || 1, c || 1, table.length, table[0].length).setValues(table);
}
// おまけ: 命名規則に沿った上で最新ぽいCSVをとってくる
// var file = get_current_csv_file(/test_(\d+)\.csv/, function(m){ return parseInt(m[1], 10) });
function get_current_csv_file(pat, order_of) {
var current = { order: "" };
var files = DriveApp.getFilesByType("text/csv");
while (files.hasNext()) {
var file = files.next();
var m = file.getName().match(pat);
if (m) {
var order = order_of(m);
if (order > current.order) {
current.order = order;
current.file = file;
}
}
}
return current.file;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment