在不使用身份验证/驱动器作用域的情况下在文件夹中创建电子表格文件 [英] Creating a spreadsheet file in a folder without using auth/drive scope

查看:46
本文介绍了在不使用身份验证/驱动器作用域的情况下在文件夹中创建电子表格文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了一个问题在创建文件并将其移动到文件夹之前,但是现在当我要发布此简单加载项时,我遇到了移动文件所需的auth/drive范围问题.身份验证/驱动器是一个敏感的范围,我只需要经过审核就可以发布.我尝试将范围更改为auth/drive.file,但显然不适用于moveTo方法.

I had asked a question before on creating a file and moving it to a folder, but now as I'm moving to publish this simple add-on I'm having issues with the auth/drive scope needed to move a file. auth/drive is a sensitive scope and I'd have to go through a review process just to publish. I tried changing the scope to auth/drive.file but apparently that doesn't apply to the moveTo method.

我感到沮丧的是,我必须跳过篮球才能在正确的位置创建文件.还有其他方法可以移动不需要敏感或受限作用域的文件夹中的文件吗?

I find it frustrating that I have to jump through hoops just to create a file in the correct location. Are there any other ways to move create a file in a folder that doesn't require sensitive or restricted scopes?

这是功能

function createSpreadsheet(form){
  var spreadsheetName = [form.getTitle() + " Results"];
  var thisFileId = form.getId();
  var parentFolder = DriveApp.getFileById(thisFileId).getParents().next();
  var spreadsheet = SpreadsheetApp.create(spreadsheetName);
  var spreadsheetID = spreadsheet.getId();
  var spreasheetFile = DriveApp.getFileById(spreadsheetID);
  spreasheetFile.moveTo(parentFolder);
  return spreadsheet;
}

推荐答案

我相信您的目标如下.

  • 您想缩小Google Apps脚本的范围.
    • 您要为此使用https://www.googleapis.com/auth/drive.file.
    • You want to narrow the scopes of your Google Apps Script.
      • You want to use https://www.googleapis.com/auth/drive.file for this.

      在这种情况下,使用驱动器服务时,需要使用https://www.googleapis.com/auth/drive的范围.因此,为了缩小范围,我建议使用Drive API代替Drive服务.

      In this case, when Drive service is used, the scope of https://www.googleapis.com/auth/drive is required to be used. So in order to narrow the scopes, I would like to propose to use Drive API instead of Drive service.

      此修改后的脚本的流程如下.

      The flow of this modified script is as follows.

      1. 获取"thisFileId"的父文件夹ID.
        • 在这种情况下,使用https://www.googleapis.com/auth/drive.metadata.readonly的范围.
      1. Retrieve the parent folder ID of "thisFileId".
        • In this case, the scope of https://www.googleapis.com/auth/drive.metadata.readonly is used.
      • 在这种情况下,使用https://www.googleapis.com/auth/drive.file的范围.
      • 使用文件:插入"方法创建电子表格并将其移动到特定文件夹.通过一个API调用.
      • In this case, the scope of https://www.googleapis.com/auth/drive.file is used.
      • The Spreadsheet is created and moved to the specific folder using the method of "Files: insert" by one API call.
      • 在这种情况下,使用https://www.googleapis.com/auth/spreadsheets的范围.
      • In this case, the scope of https://www.googleapis.com/auth/spreadsheets is used.

      此流程与您的脚本相同.当上述流程反映到您的脚本中时,它如下所示.

      This flow is the same process with your script. When above flow is reflected to your script, it becomes as follows.

      在使用此脚本之前,请请在高级Google上启用Drive API服务.

      Before you use this script, please enable Drive API at Advanced Google services.

      function createSpreadsheet(form){
        var spreadsheetName = [form.getTitle() + " Results"];
        var thisFileId = form.getId();
        
        // 1. Retrieve the parent folder ID of "thisFileId".
        // In this case, the scope of "https://www.googleapis.com/auth/drive.metadata.readonly" is used.
        var folderId = Drive.Files.get(thisFileId).parents[0].id;
        
        // 2. Create new Spreadsheet to the specific folder.
        // In this case, the scope of "https://www.googleapis.com/auth/drive.file" is used.
        var spreadsheetId = Drive.Files.insert({title: spreadsheetName, mimeType: MimeType.GOOGLE_SHEETS, parents: [{id: folderId}]}).id;
        
        // 3. Retrieve Spreadsheet object.
        // In this case, the scope of "https://www.googleapis.com/auth/spreadsheets" is used.
        return SpreadsheetApp.openById(spreadsheetId);
      }
      

      注意:

      • 此示例脚本适用于您所提问的脚本.因此,当您在另一部分中使用使用https://www.googleapis.com/auth/drive范围的方法时,此脚本可能没有用.请注意这一点.
      • form.getTitle()form.getId()中,可能需要包含https://www.googleapis.com/auth/forms的范围.请注意这一点.另外,当您使用其他方法使用其他范围时,请包括在内.
      • 使用https://www.googleapis.com/auth/drive.file范围创建电子表格,应用程序可以使用该范围使用电子表格.请注意这一点.
      • Note:

        • This sample script is for the script in your question. So when you are using the methods for using the scope of https://www.googleapis.com/auth/drive in your other part, this script might not be useful. Please be careful this.
        • From form.getTitle() and form.getId(), the scope of https://www.googleapis.com/auth/forms might be required to be included. Please be careful this. And also, when you are using other methods for using other scopes, please include them.
        • The Spreadsheet is created with the scope of https://www.googleapis.com/auth/drive.file, the Spreadsheet can be used by the application using the scope. Please be careful this.
          • Files: get
          • Files: insert

          这篇关于在不使用身份验证/驱动器作用域的情况下在文件夹中创建电子表格文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆