我一直试图将多个csv文件从Google驱动器文件夹合并到单个csv文件中,但是我无法做到这一点,因为它可以很好地与google工作表一起使用,但不适用于csv文件。
我使用了stackoverflow.com上已经可用的代码,但不能很好地满足我的要求。
function mergeSheets() {
/* Retrieve the desired folder */
var myFolder = DriveApp.getFoldersByName("Test 1").next();
/* Get all spreadsheets that resided on that folder */
var spreadSheets = myFolder.getFilesByType("MimeType.csv");
/* Create the new spreadsheet that you store other sheets */
var newSpreadSheet = SpreadsheetApp.create("Merged Sheets");
/* Iterate over the spreadsheets over the folder */
while(spreadSheets.hasNext()) {
var sheet = spreadSheets.next();
/* Open the spreadsheet */
var spreadSheet = SpreadsheetApp.openById(sheet.getId());
/* Get all its sheets */
for(var y in spreadSheet.getSheets()) {
/* Copy the sheet to the new merged Spread Sheet */
spreadSheet.getSheets()[y].copyTo(newSpreadSheet);
}
}
}
它确实创建了一个新工作表,但是没有任何csv数据。
答案 0 :(得分:5)
这是我的解决方案:
尝试一下-
function mergeSheets() {
/* Retrieve the desired folder */
var myFolder = DriveApp.getFoldersByName("Test 1").next();
/* Get all spreadsheets that resided on that folder */
var spreadSheets = myFolder.getFilesByType(MimeType.CSV);
/* Create the new spreadsheet that you store other sheets */
var newSpreadSheet = SpreadsheetApp.create("Merged Sheets");
/* Iterate over the spreadsheets over the folder */
while(spreadSheets.hasNext()) {
var sheet = spreadSheets.next();
var file = DriveApp.getFileById(sheet.getId());
var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
var newSheet = newSpreadSheet.getActiveSheet();
newSheet.getRange(newSpreadSheet.getLastRow() + 1, newSpreadSheet.getLastColumn()+1, csvData.length, csvData[0].length).setValues(csvData);
}
}
我假设您必须从这里开始了解原始代码-Merge multiple tabs, download as CSV
我从这篇here的文章中找到了灵感。
答案 1 :(得分:1)
解决方案的主要问题是:
"MimeType.csv"
与正确的"text/csv"
。或者,仅MimeType.csv
不带引号也可以。)要解决我遇到的这些问题,
SpreadsheetApp.openById(sheet.getId());
切换为Utilities.parseCsv(sheet.getBlob().getDataAsString(), ",");
,以便我们可以正确open a csv file。这里有一些代码可以将多个csv文件放入单个电子表格中,每个文件都在自己的工作表中。
function mergeSheets() {
/* Retrieve the desired folder */
var myFolder = DriveApp.getFoldersByName("Test 1").next();
/* Get all spreadsheets that resided on that folder */
var spreadSheets = myFolder.getFilesByType("text/csv");
if (spreadSheets.hasNext()) {
/* Create the new spreadsheet that you store other sheets */
var newSpreadSheet = SpreadsheetApp.create("Merged Sheets");
/* Iterate over the spreadsheets over the folder */
while(spreadSheets.hasNext()) {
var sheet = spreadSheets.next();
/* Load the csv data */
var csvData = Utilities.parseCsv(sheet.getBlob().getDataAsString(), ",");
/* Copy the sheet to the new merged Spread Sheet */
var newSheet = newSpreadSheet.insertSheet("import_"+sheet.getName());
for (var i=0;i<csvData.length;i++) {
newSheet.appendRow(csvData[i]);
}
}
}
}
希望这会有所帮助!
答案 2 :(得分:0)
function mergeCSVFiles(folderId,filename) {
var folder=DriveApp.getFolderById(folderId);
var files=folder.getFilesByType(MimeType.csv);
var s='';
while(files.hasNext()) {
var file=files.next();
s+=file.getBlob().getDataAsString();
}
var currentFiles=DriveApp.getRootFolder().getFilesByName(filename);
while(currentFiles.hasNext()) {
currentFiles.next().setTrashed(true);
}
DriveApp.getRootFolder().createFile(filename,s,”text/csv”);//The merged file is in your root folder.
}