![見出し画像](https://assets.st-note.com/production/uploads/images/62367845/rectangle_large_type_2_6692c41bcbe211569a3f28b28c0b7b01.jpeg?width=1200)
[GAS]Excelファイルをスプレッドシートに転記
以前「CSVファイルをスプレッドシートに転記」という記事を書きました。
今回は「Excelファイルをスプレッドシートに転記」という手順をメモします。
こちらも私が所属しているコミュニティで教えて頂きました。ありがとうございました。
例えば下記のように「Excelファイル」というGoogleドライブフォルダに、
Excelファイルが1つあったとします。
このファイルを開き、データをスプレッドシートに丸々転記したいとします。
![画像1](https://assets.st-note.com/production/uploads/images/62343608/picture_pc_ab1f446694f06775a30daae4cb2875a1.png?width=1200)
先に、Drive APIを有効にします。
手順は以下の通りです。
1_「サービス」横のプラスを押下
![画像2](https://assets.st-note.com/production/uploads/images/62347981/picture_pc_312f88dbba0eab034899b332f7b710f7.png?width=1200)
2_Drive APIドキュメントを選択し「追加」押下
![画像4](https://assets.st-note.com/production/uploads/images/62348018/picture_pc_4eb036fdd4ec07dfd898094611e50d1d.png?width=1200)
Drive APIが追加されました。
![画像5](https://assets.st-note.com/production/uploads/images/62348081/picture_pc_f1832e9d5b0df23da0d9dbe3f0253d7f.png)
スクリプトはこちら。
function EXCELtoSS() {
const folder = DriveApp.getFolderById('/***Folder_ID***/');//Excelを格納したフォルダのID
const files = folder.getFiles();
const file = files.next();
options = {
title : file.getName(),
mimeType: MimeType.GOOGLE_SHEETS,
parents : [{ id: folder.getId() }]
}
const newSs = SpreadsheetApp.openById(Drive.Files.insert(options, file.getBlob()).id); //ExcelをSSに変換し取得
const newSheet = newSs.getActiveSheet();
const values = newSheet.getDataRange().getValues();
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
今回も解説できるほど理解できておりません...すいません。
Excelファイルをスプレッドシートに変換してから転記する、ということでしょうかね。
フォルダ内には同名のスプレッドシートファイルが生成され、
![画像5](https://assets.st-note.com/production/uploads/images/62362197/picture_pc_7f87ba88365068af332c7a89abf94ff4.png?width=1200)
スプレッドシートに転記されました。
![画像6](https://assets.st-note.com/production/uploads/images/62362269/picture_pc_9f358676d2dba042649f8d43bd1758b5.png)
★この記事同様、複数ファイルをまとめるパターンも記します。
下記のように、「Excelファイル」Googleドライブフォルダの中に、
Excelファイルが3つあるとします。
その他に「使用済みファイル」フォルダがあります。
![画像7](https://assets.st-note.com/production/uploads/images/62364622/picture_pc_26aec1c79b0fc509e7f099249b0a4ad9.png?width=1200)
![画像8](https://assets.st-note.com/production/uploads/images/62364628/picture_pc_7b091c2ff15bb73f99cca0a7bd0d8ec8.png?width=1200)
スクリプトはこちら。
function EXCELtoSS() {
const folder = DriveApp.getFolderById('/***Folder_ID***/');//Excelを格納したフォルダのID
const moveDir = DriveApp.getFolderById('/***Folder_ID***/');//使用済みフォルダ(上記フォルダ内)
const files = folder.getFiles();
let allValues = [];
while (files.hasNext()) {
const file = files.next();
options = {
title : file.getName(),
mimeType: MimeType.GOOGLE_SHEETS,
parents : [{ id: folder.getId() }]
}
const newSs = SpreadsheetApp.openById(Drive.Files.insert(options, file.getBlob()).id); //ExcelをSSに変換し取得
const newSheet = newSs.getActiveSheet();
const values = newSheet.getDataRange().getValues();
values.shift();
allValues = allValues.concat(values);//変数allValuesに各csvデータを追加
}
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(2, 1, allValues.length, allValues[0].length).setValues(allValues);
const moveFiles = folder.getFiles(); //フォルダ内のファイルを一括取得
for (let i = 0; moveFiles.hasNext(); i++) {
let moveFile = moveFiles.next();
moveFile.moveTo(moveDir);
}
}
スクリプトを実行すると、元のスプレッドシートに3ファイルをまとめたデータが転記されます。
![画像9](https://assets.st-note.com/production/uploads/images/62367716/picture_pc_deeceefdaf8843e71c26cf964ba279eb.png)
「使用済みフォルダ」にすべてのExcelファイル(変換されたスプレッドシートファイル含め)が移動されています。
![画像10](https://assets.st-note.com/production/uploads/images/62367765/picture_pc_f402fb493f10c0b5234cb06e45db36c4.png?width=1200)
![画像11](https://assets.st-note.com/production/uploads/images/62367774/picture_pc_fe49bc3b9a1cc853d79c97df8fd3045b.png?width=1200)
※2022年9月に技術同人誌「会社員がVLOOKUPの次に覚えるQUERY関数超入門」を出版いたしました。