首页 > 解决方案 > 从谷歌驱动器转换所有 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});
}

标签: javascriptgoogle-apps-scriptgoogle-drive-api

解决方案


如果您上面列出的代码已经在一次检索单个 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});
}

这回答了你的问题了吗?


推荐阅读