将XLS转换为CSV,以便;新";仅文件
我有:将XLS转换为CSV,以便;新";仅文件,csv,google-apps-script,google-sheets,google-drive-api,xlsx,Csv,Google Apps Script,Google Sheets,Google Drive Api,Xlsx,我有: .xlsx文件每天自动导入到google 驱动器文件夹 我想要: 每个文件都转换为CSV并保存到不同的文件夹 每个文件都转换为Google工作表并保存到不同的文件夹 未删除原始xlsx文件 但仅适用于自上次运行脚本以来添加的文件 我把下面的代码拼凑在一起。然而,我有一个问题——当我尝试运行它时,我收到一条错误消息 GoogleJsonResponseException:API调用drive.files.copy失败,错误为:找不到文件:[SheetsFolder的名称] 有人能通
- .xlsx文件每天自动导入到google 驱动器文件夹
- 每个文件都转换为CSV并保存到不同的文件夹
- 每个文件都转换为Google工作表并保存到不同的文件夹
- 未删除原始xlsx文件
- 但仅适用于自上次运行脚本以来添加的文件
function makeCSV() {
//Add folder ID to select the folder where excel files are placed
var SourceFolder = DriveApp.getFolderById("12XqAVzwzgl4a7Z1s6zn5VRcHiZ1vQdsT")
//Add folder ID to save the where csv files to be placed
var DestinationFolder = DriveApp.getFolderById("1Z_mUXs3FFDTN7XLk8EN9D5QOxVD8lZfB")
//Add folder ID to save where the google sheets to be placed
var SheetsFolder = DriveApp.getFolderById("17k_bl7Of2jH3U_TjaiiY_8BBOds4uSbT")
//Select the XLS and XLSX files from source folder using the Mimetype of Microsoft Excel
var searchQuery = "mimeType='" + MimeType.MICROSOFT_EXCEL + "' or mimeType='" + MimeType.MICROSOFT_EXCEL_LEGACY + "'";
var sourceFiles = SourceFolder.searchFiles(searchQuery);
var now = new Date(); //get current time after you fetch the file list from Drive.
//Get script properties and check for stored "last_execution_time"
var properties = PropertiesService.getScriptProperties();
var cutoff_datetime = properties.getProperty('last_execution_time');
//if we have last execution date, stored as a string, convert it to a Date object.
if(cutoff_datetime)
cutoff_datetime = new Date(cutoff_datetime);
//Loop over all the XLS files
while (sourceFiles.hasNext()){
var sourceFile = sourceFiles.next();
//if no stored last execution, or file is newer than last execution, process the file.
if(!cutoff_datetime || file.getDateCreated() > cutoff_datetime){
var fileId = sourceFile.getId();
//Copy the XLS file to the Sheets Folder as a google sheet
var spreadsheet = Drive.Files.copy({mimeType: MimeType.GOOGLE_SHEETS, parents: [{id: SheetsFolder}]}, fileId);
//Get the data from the google sheet using the SpreadsheetApp
var sheets = SpreadsheetApp.openById(spreadsheet.id).getSheets();sheets[0].getDataRange().getValues()
//Create a csv file using the data from the google sheet
var csv = sheets.map(function(sheet) {return sheet.getDataRange().getValues().reduce(function(csv, row) {return csv += row.join(",") + "\n"}, "")}); DestinationFolder.createFile(spreadsheet.title + ".csv", csv, MimeType.CSV)
}
}
//store "now" as last execution time as a string, to be referenced on next run.
properties.setProperty('last_execution_time',now.toString());
}
非常感谢任何帮助
编辑1:感谢Tanaike帮助解决问题!上述代码有两个问题:一个是塔奈克在评论中指出的问题。第二个是“file”应该在if语句中读取“sourceFile”。我在下面为子孙后代发布正确的代码
function makeCSV() {
//Add folder ID to select the folder where excel files are placed
var SourceFolder = DriveApp.getFolderById("12XqAVzwzgl4a7Z1s6zn5VRcHiZ1vQdsT")
//Add folder ID to save the where csv files to be placed
var DestinationFolder = DriveApp.getFolderById("1Z_mUXs3FFDTN7XLk8EN9D5QOxVD8lZfB")
//Add folder ID to save where the google sheets to be placed
var SheetsFolder = DriveApp.getFolderById("17k_bl7Of2jH3U_TjaiiY_8BBOds4uSbT")
//Select the XLS and XLSX files from source folder using the Mimetype of Microsoft Excel
var searchQuery = "mimeType='" + MimeType.MICROSOFT_EXCEL + "' or mimeType='" + MimeType.MICROSOFT_EXCEL_LEGACY + "'";
var sourceFiles = SourceFolder.searchFiles(searchQuery);
var now = new Date(); //get current time after you fetch the file list from Drive.
//Get script properties and check for stored "last_execution_time"
var properties = PropertiesService.getScriptProperties();
var cutoff_datetime = properties.getProperty('last_execution_time');
//if we have last execution date, stored as a string, convert it to a Date object.
if(cutoff_datetime)
cutoff_datetime = new Date(cutoff_datetime);
//Loop over all the XLS files
while (sourceFiles.hasNext()){
var sourceFile = sourceFiles.next();
//if no stored last execution, or file is newer than last execution, process the file.
if(!cutoff_datetime || sourceFile.getDateCreated() > cutoff_datetime){
var fileId = sourceFile.getId();
//Copy the XLS file to the Sheets Folder as a google sheet
var spreadsheet = Drive.Files.copy({mimeType: MimeType.GOOGLE_SHEETS, parents: [{id:"17k_bl7Of2jH3U_TjaiiY_8BBOds4uSbT"}]}, fileId);
//Get the data from the google sheet using the SpreadsheetApp
var sheets = SpreadsheetApp.openById(spreadsheet.id).getSheets();sheets[0].getDataRange().getValues()
//Create a csv file using the data from the google sheet
var csv = sheets.map(function(sheet) {return sheet.getDataRange().getValues().reduce(function(csv, row) {return csv += row.join(",") + "\n"}, "")}); DestinationFolder.createFile(spreadsheet.title + ".csv", csv, MimeType.CSV)
}
}
//store "now" as last execution time as a string, to be referenced on next run.
properties.setProperty('last_execution_time',now.toString());
}
从
parents:[{id:SheetsFolder}]}
修改为parents:[{id:[17k_bl72jh3u_tjaiy_8BBOds4uSbT}]}
怎么样?因为需要使用文件夹ID来驱动.Files.copy
而不是文件夹对象。但我不确定你的问题是否只是这个。所以我提出这个作为评论。谢谢塔奈克!代码有两个“是”。1.)您提到的问题和2.)if语句中的应为“sourceFile.getDateCreated()”。现在可以了!从parents:[{id:SheetsFolder}]}
修改为parents:[{id:[17k_bl72jh3u_tjaiy_8BBOds4uSbT}]}
怎么样?因为需要使用文件夹ID来驱动.Files.copy
而不是文件夹对象。但我不确定你的问题是否只是这个。所以我提出这个作为评论。谢谢塔奈克!代码有两个“是”。1.)您提到的问题和2.)if语句中的应为“sourceFile.getDateCreated()”。现在可以了!