0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GAS//Driveのフォルダに格納したCSVを自動転記

Last updated at Posted at 2020-03-18

やりたかったこと

GoogleDriveに格納したファイルを一定時間ごとにチェック(←これはトリガーでできる)
特定の返品理由のユーザーのみリストにする(電話して確認するため)
リストにしたファイルは「転記済」フォルダに移す

importCSV
var ss = SpreadsheetApp.openById("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
var sheets = ss.getSheets();
var telList = ss.setActiveSheet(sheets[0]);
var date = new Date();

function importCSV() {
  //取得ファイルとディレクトリ
    try {
  var files = DriveApp.getFolderById('xxxxxxxxxxxxxxxxxxxxxxxxxxx').getFiles();
  while(files.hasNext()) {
      var file = files.next();
       Logger.log(files);
       Logger.log(file);
      var data = file.getBlob().getDataAsString("Shift_JIS");
      var csv = Utilities.parseCsv(data);
   
      //指定の列のみシートに移す
      var col1 = Utilities.formatDate(date, 'Asia/Tokyo', 'yyyy/MM/dd hh:mm');
      for (var i = 0; i < csv.length; i++) {
         var col2 = csv[i][7];
         var col3 = csv[i][10];
         var col4 = csv[i][11];
         var col5 = csv[i][14];
         var col6 = csv[i][15];
         var col7 = csv[i][33];
         var col8 = csv[i][34];

        if( ( col7 % 2 ) == 0 ) {// 返品理由区分IDが偶数のデータだけリストにする
         telList.appendRow([col1,col2,col3,col4,col5,col6,col7,col8]);
          }
      }
      
    //転記が終わったらファイルを転記済フォルダに格納
    var destfolder = DriveApp.getFoldersByName('架電リスト転記済').next();
    var sourcefolder = file.getParents().next();
    destfolder.addFile(file);
    sourcefolder.removeFile(file); 
     }
      } catch(e) {
      }
}
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?