pm2*_*588 5 file-conversion google-sheets delete-file google-apps-script google-drive-api
我知道可以使用脚本和驱动 API 将 Excel 文件转换为 Google Sheets,但我正在寻找脚本来转换 Excel 工作表并将转换后的文件移动到其他文件夹。
所以需要的步骤如下:
Excel 文件正在粘贴到正在同步到 Google Drive 的本地文件夹中,并且文件不大于 3mb。当前脚本如下。这是转换文件,但将它们放置在根文件夹中,并且当脚本再次运行时将重复转换。
function importXLS(){
var files = DriveApp.getFolderById('1hjvNIPgKhp2ZKIC7K2kxvJjfIeEYw4BP').searchFiles('title != "nothing"');
while(files.hasNext()){
var xFile = files.next();
var name = xFile.getName();
if (name.indexOf('.xlsx')>-1){
var ID = xFile.getId();
var xBlob = xFile.getBlob();
var newFile = { title : name+'_converted',
key : ID
}
file = Drive.Files.insert(newFile, xBlob, {
convert: true
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我的理解正确的话,这个修改怎么样?在这次修改中,我修改了你的脚本。
parents您可以使用请求正文中的属性直接将文件创建到特定文件夹。Drive.Files.remove(fileId)。在使用此脚本之前,请在高级 Google 服务中启用 Drive API。
function importXLS(){
var folderBId = "###"; // Added // Please set the folder ID of "FolderB".
var files = DriveApp.getFolderById('1hjvNIPgKhp2ZKIC7K2kxvJjfIeEYw4BP').searchFiles('title != "nothing"');
while(files.hasNext()){
var xFile = files.next();
var name = xFile.getName();
if (name.indexOf('.xlsx')>-1){
var ID = xFile.getId();
var xBlob = xFile.getBlob();
var newFile = {
title : name+'_converted',
parents: [{id: folderBId}] // Added
};
file = Drive.Files.insert(newFile, xBlob, {
convert: true
});
// Drive.Files.remove(ID); // Added // If this line is run, the original XLSX file is removed. So please be careful this.
}
}
}
Run Code Online (Sandbox Code Playgroud)
// Drive.Files.remove(ID);,当你运行这个脚本时,请小心。因为脚本运行时原始XLSX文件被完全删除。所以我评论了这一点。首先,请使用示例文件测试脚本。| 归档时间: |
|
| 查看次数: |
15136 次 |
| 最近记录: |