google-apps-script - 将文件夹中所有可用的 xls 文件转换为 "Google Doc Spreadsheets"?

标签 google-apps-script google-sheets google-drive-api

执行此操作的基本方法是在将文件上传到 Google 云端硬盘时开启转换。

另一种方法是选择文件夹中的xls文件,并手动对其进行转换。

但是,如果一个文件夹中已经上传了许多 xls 文件,则使用 Google Apps 脚本转换它们可能比重新上传文件更快。

就我而言:

  • 转换后,我需要删除 xls 文件
  • 所有 xls 文件都低于限制:“上传的转换为 Google 电子表格格式的电子表格文件不能大于 100 MB,并且每张表格需要少于 400,000 个单元格和 256 列。”
    https://support.google.com/drive/answer/37603?hl=en

  • 提前致谢;)

    最佳答案

    您应该使用 Advanced Drive Service , file update

    此服务必须在使用前启用,但文档非常清楚。

    编辑(按照您的意见)

    (抱歉耽误了这么久,我忘记了这个帖子)

    此代码会将您驱动器中的每个 XLS 文件转换为 Google 电子表格格式(只要它们的名称具有 .xls 扩展名)

    您必须授权 Drive 扩展资源 + API 控制台(按照资源/高级服务菜单中的说明进行操作,参见下图)

    function importXLS(){
      var files = DriveApp.searchFiles('title contains ".xls"');// you can also use a folder as starting point and get the files in that folder... use only DriveApp method here.
      while(files.hasNext()){
        var xFile = files.next();
        var name = xFile.getName();
        if (name.indexOf('.xls')>-1){ // this check is not necessaey here because I get the files with a search but I left it in case you get the files differently...
          var ID = xFile.getId();
          var xBlob = xFile.getBlob();
          var newFile = { title : name+'_converted',
                         key : ID
                        }
          file = Drive.Files.insert(newFile, xBlob, {
            convert: true
          });
        }
      }
    }
    

    enter image description here

    关于google-apps-script - 将文件夹中所有可用的 xls 文件转换为 "Google Doc Spreadsheets"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25368143/

    相关文章:

    javascript - Sleep() 未按预期工作

    javascript - 谷歌脚本: How to get Recurring Events from Google Calendar?

    javascript - 重新配置匹配函数 Google 脚本的输出第 2 部分

    google-sheets - 根据 A 列中的日期格式化整行中的文本/单元格

    javascript - 尝试在 Google Apps 脚本中获取文件的文件夹路径

    java - 从 AppEngine Java 将 byte[] 保存到 Google Drive

    python - 使用 Python 访问 Google 电子表格以供服务器到服务器使用

    google-sheets - 是否有 Google Sheets 公式可以将工作表的名称放入单元格中?

    javascript - 尝试执行 google Apps 脚本时的错误值

    ios - 谷歌云端硬盘 iOS SDK : Getting user's space available