转换Google云端硬盘中的所有csv

时间:2018-12-18 12:41:21

标签: javascript google-apps-script google-drive-api

我正尝试在Google驱动器中搜索所有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});
}

1 个答案:

答案 0 :(得分:1)

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

这能回答您的问题吗?