我有:
我想要:
当前我的流程是这样
我最初是使用此https://ctrlq.org/code/20248-convert-excel-to-csv教程转换为CSV的,直到意识到它将每个.xlsx表的“无标题”副本作为Drive电子表格保存到我的根文件夹中。我不知道如何为未命名的人分配标题或文件夹位置。能够做到这一点还可以解决我眼前的问题。
然后,我尝试使用https://ctrlq.org/code/20500-convert-microsoft-excel-xlsx-to-google-spreadsheet的此修改版本(以下)和MimeType .CSV,将其CSV正确放置在具有正确名称的正确文件夹中,但是数据未正确解析并且看上去已损坏
function exceltoSheets() {
var SOURCE_XLS_FOLDER = '123465';
var SHEET_FOLDER = '789456';
var CSV_TEST = '456123';
var sourceFolderID = DriveApp.getFolderById(SOURCE_XLS_FOLDER),
mimes = [MimeType.MICROSOFT_EXCEL, MimeType.MICROSOFT_EXCEL_LEGACY];
for (var m = 0; m < mimes.length; m++) {
var sourceFiles = sourceFolderID.getFilesByType(mimes[m]);
while (sourceFiles.hasNext()) {
try {
var sourceFile = sourceFiles.next();
var sourceName = sourceFile.getName().replace(".xlsx","");
var sourceNameTC = sourceName + ".csv"
var sourceNameDS = "ds_data_import_" + sourceName;
var fileId = sourceFile.getId();
var blob = sourceFile.getBlob();
var resourceDS = {
title: sourceNameDS,
mimeType: MimeType.GOOGLE_SHEETS,
convert: true,
parents: [{id: SHEET_FOLDER}]
};
var resourceTC = {
title: sourceNameTC,
mimeType: MimeType.CSV,
convert: true,
parents: [{id: CSV_TEST}],
};
Drive.Files.insert(resourceDS, blob);
Drive.Files.insert(resourceTC, blob);
} catch (f) {
Logger.log(f.toString());
}
sourceFile.setTrashed(true);
}
}
}
如果我正确解析了CSV,则最终会在根文件夹中出现“无标题”图纸,如果我正确解析了“图纸”,则最终会导致CSV损坏。我想要结果:
答案 0 :(得分:1)
如果我的理解是正确的,那么该修改如何?请认为这只是几个答案之一。
MimeType.MICROSOFT_EXCEL
和MimeType.MICROSOFT_EXCEL_LEGACY
的文件,我使用了searchFiles()
。Drive.Files.copy()
。使用此功能时,请确认是否在高级Google服务中启用了Drive API。
function exceltoSheets() {
var SOURCE_XLS_FOLDER = '###'; // Please set the source folder ID here.
var dstFolderId = '####'; // Please set the destination folder ID here.
var sourceFolderID = DriveApp.getFolderById(SOURCE_XLS_FOLDER);
var searchQuery = "mimeType='" + MimeType.MICROSOFT_EXCEL + "' or mimeType='" + MimeType.MICROSOFT_EXCEL_LEGACY + "'";
var sourceFiles = sourceFolderID.searchFiles(searchQuery);
while (sourceFiles.hasNext()) {
var sourceFile = sourceFiles.next();
var fileId = sourceFile.getId();
Drive.Files.copy({mimeType: MimeType.GOOGLE_SHEETS, parents: [{id: dstFolderId}]}, fileId);
sourceFile.setTrashed(true);
}
}
Drive.Files.remove(fileId)
代替sourceFile.setTrashed(true)
。在这种情况下,修改后的脚本如下。
function exceltoSheets() {
var SOURCE_XLS_FOLDER = '###'; // Please set the source folder ID here.
var dstFolderIdForSpreadsheet = '###'; // Please set the destination folder ID for Spreadsheet here.
var dstFolderIdForCSV = '###'; // Please set the destination folder ID for CSV here.
var sourceFolder = DriveApp.getFolderById(SOURCE_XLS_FOLDER);
var destinationFolderForCSV = DriveApp.getFolderById(dstFolderIdForCSV);
var searchQuery = "mimeType='" + MimeType.MICROSOFT_EXCEL + "' or mimeType='" + MimeType.MICROSOFT_EXCEL_LEGACY + "'";
var sourceFiles = sourceFolder.searchFiles(searchQuery);
while (sourceFiles.hasNext()) {
var sourceFile = sourceFiles.next();
var fileId = sourceFile.getId();
var spreadsheet = Drive.Files.copy({mimeType: MimeType.GOOGLE_SHEETS, parents: [{id: dstFolderIdForSpreadsheet}]}, fileId);
var sheets = SpreadsheetApp.openById(spreadsheet.id).getSheets();
sheets[0].getDataRange().getValues()
var csv = sheets.map(function(sheet) {return sheet.getDataRange().getValues().reduce(function(csv, row) {return csv += row.join(",") + "\n"}, "")});
destinationFolderForCSV.createFile(spreadsheet.title + ".csv", csv, MimeType.CSV)
sourceFile.setTrashed(true);
}
}