【问题标题】:Upload a word doc and an google app script. Convert the word doc to a google doc and then run the app script clean up on it?上传 word doc 和 google app 脚本。将 word doc 转换为 google doc,然后运行 ​​app 脚本清理它?
【发布时间】:2020-02-25 16:19:18
【问题描述】:

我可以上传文件并使用负责转换的 Google Drive v3 进行转换。

function uploadFile(auth) {
  const drive = google.drive({version: 'v3', auth});
  var fileMetadata = {
    'name': 'My Uploaded Test Word File',
    'mimeType': 'application/vnd.google-apps.document'
  };
  var media = {
    mimeType: 'application/msword',
    body: fs.createReadStream('./test-word.doc')
  };
  drive.files.create({
    resource: fileMetadata,
    media: media,
    fields: 'id'
  }, function (err, file) {
    if (err) {
      // Handle error
      console.error(err);
    } else {
      console.log('File Id:', file.id);
    }
  });
}

问题是,转换会导致一些需要清理的异常情况,并且只能使用 Google 应用脚本来完成 - 例如删除书签。

想知道在转换后的 word doc 上运行 Google Apps 脚本的最佳方法是什么。我可以上传一个应用脚本文件,然后在转换完成后触发它运行吗?

不胜感激。

【问题讨论】:

    标签: google-apps-script ms-word google-drive-api google-docs


    【解决方案1】:
    • 您希望在将 DOC 文件(Microsoft Doc 文件)作为 Google 文档上传后运行 Google Apps Script 的功能。
    • 您想使用 googleapis 和 Node.js 来上传 DOC 文件
    • 您想使用 Google Apps 脚本对转换后的 Google 文档进行后期处理。
    • 您已经能够使用 Drive API 上传 DOC 文件。

    如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。

    在这个答案中,我想在文件上传后使用 Web Apps 运行 Google Apps 脚本的功能。您也可以使用 Apps Script API 运行 Google Apps Script 的功能。但在这种情况下,您使用的是 OAuth2。所以我认为使用 Web 应用程序很容易安全地实现您的目标。该示例脚本的流程如下。

    流程:

    1. 使用 googleapis 将 DOC 文件作为 Google 文档上传。
    2. 通过向 Web Apps 请求运行 Google Apps Script 的功能。

    用法:

    为了使用它,请执行以下流程。

    1。创建 Google Apps 脚本的新项目。

    Web Apps 的示例脚本是 Google Apps 脚本。所以请创建一个 Google Apps Script 项目。

    根据您的问题,我可以确认您已经拥有 GAS 项目。所以请使用它。

    2。复制并粘贴脚本。

    请复制并粘贴以下脚本。

    function doGet(e) {
      var fileId = e.parameter.id;  // You can retrieve the file ID from Node.js using this.
    
      // do something: Please set the function you want to run.
    
      return ContentService.createTextOutput("Done.");
    }
    

    3。部署 Web 应用程序。

    1. 在脚本编辑器上,通过“发布”->“部署为 Web 应用”打开一个对话框。
    2. “执行应用程序为:”选择“我”
    3. “谁有权访问应用程序:”选择“仅限我自己”
      • 在此设置中,访问令牌用于访问 Web 应用程序。
      • 当“任何人,甚至匿名”设置为“谁有权访问应用程序:”时,不需要使用访问令牌。
    4. 单击“部署”按钮作为新的“项目版本”。
    5. 自动打开“需要授权”对话框。
      1. 点击“查看权限”。
      2. 选择自己的帐户。
      3. 点击“此应用未验证”中的“高级”。
      4. 点击“转到###项目名称###(不安全)”
      5. 点击“允许”按钮。
    6. 点击“确定”。
    7. 复制 Web 应用程序的 URL。就像https://script.google.com/macros/s/###/exec
      • 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。

    4。修改后的脚本:

    当您的脚本被修改时,请进行如下修改。并且请设置 Web Apps 的 URL。

    function uploadFile(auth) {
      const drive = google.drive({ version: "v3", auth });
    
      var fileMetadata = {
        name: "My Uploaded Test Word File",
        mimeType: "application/vnd.google-apps.document"
      };
      var media = {
        mimeType: "application/msword",
        body: fs.createReadStream("./test-word.doc")
      };
      drive.files.create(
        {
          resource: fileMetadata,
          media: media,
          fields: "id"
        },
        function(err, file) {
          if (err) {
            // Handle error
            console.error(err);
          } else {
            const fileId = file.data.id;
            console.log("File Id:", fileId);
    
            // The below script requests to Web Apps.
            const request = require("request");
            auth.getRequestHeaders().then(authorization => {
              request(
                {
                  url: `https://script.google.com/macros/s/###/exec?id=${fileId}`,  // Please set this.
                  method: "GET",
                  followAllRedirects: true,
                  headers: authorization
                },
                function(err, res, body) {
                  console.log(body);
                }
              );
            });
          }
        }
      );
    }
    
    • 在您的脚本中,可以使用file.data.id 直接检索文件ID。
    • Web Apps脚本执行完毕,返回Done.

    注意:

    • 当您修改 Web Apps 的 Google Apps 脚本时,请将 Web Apps 重新部署为新版本。这样,最新的脚本就会反映到 Web 应用程序中。请注意这一点。

    参考资料:

    如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

    【讨论】:

    • 太棒了!在我将请求模块添加到节点设置后,它工作得很好。谢谢,Tanaike 花时间解释得这么清楚。
    • @Heartful Dodger 感谢您的回复。很高兴您的问题得到解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 2017-07-20
    • 1970-01-01
    • 2020-10-01
    相关资源
    最近更新 更多