javascript - 从谷歌驱动器转换所有 csvs
问题描述
我正在尝试在我的谷歌驱动器中搜索所有 csv,并将它们存储在具有电子表格格式的特定文件夹中。
我已经成功地为特定的 csv 按名称尝试了相同的方法,但现在我想要所有 csv,但我不知道该怎么做。
function convert() {
var file = DriveApp.searchFiles("*filename*").next();
var name = file.getName();
var ID = file.getId();
var xBlob = file.getBlob();
var newFile = { title : name+'_converted',
key : ID,
parents: [{"id": "**folder id**"}]
}
file = Drive.Files.insert(newFile,xBlob, {convert: true});
}
解决方案
如果您上面列出的代码已经在一次检索单个 CSV 文件,您需要做的就是将其包装在一个循环中,该循环将继续运行文件,直到它们都被处理完。它应该看起来像这样:
// You will need to use the syntax in Google's developer guide to search with searchFiles:
// https://developers.google.com/apps-script/reference/drive/drive-app#searchfilesparams
// This should find any files with the text '.csv'. If there are false positives, you can change this to 'mimeType contains "csv"' to look for files with either application/csv or text/csv mime types
var files = DriveApp.searchFiles('title contains ".csv"');
// The code inside the while statement will run on each file until there are no more files in the array from searchFiles
while (files.hasNext()) {
// Gets the next file in the array of files
var file = files.next();
var name = file.getName();
var ID = file.getId();
var xBlob = file.getBlob();
var newFile = { title : name+'_converted',
key : ID,
parents: [{"id": "**folder id**"}]
}
file = Drive.Files.insert(newFile,xBlob, {convert: true});
}
这回答了你的问题了吗?
推荐阅读
- redux - Redux 状态在下一个 js 调度后不会改变
- php - 公式错误:公式错误:非法字符 '~'
- swift - Directly assign UnsafePointer
- python - 我无法在 cmd 中加载 python 库,但它们在 Anaconda 提示符下工作
- c++ - 处理大型 2D 阵列的小型 2D 部分并在其周围移动
- asp.net-mvc - 传递大数据时出现 500 内部服务器错误
- basic-authentication - 阿帕奇 2.2。基本身份验证,强制提示
- sql-server - 如何在 SQL Server 中解析带有标题和行的 SOAP XML 并显示为表?
- highcharts - Highcharts:仅将最后一行设置为虚线区域
- excel - MS Excel:如果单元格包含文本,则在同一工作表中复制一个单元格