Google apps script 在文件夹中创建电子表格文件而不使用auth/drive作用域
我以前问过一个关于创建文件并将其移动到文件夹的问题,但是现在,当我要发布这个简单的附加组件时,我遇到了移动文件所需的auth/drive范围的问题。auth/drive是一个敏感的作用域,我必须经过一个审查过程才能发布。我尝试将作用域更改为auth/drive.file,但显然这不适用于moveTo方法 为了在正确的位置创建一个文件,我不得不跳转,这让我感到沮丧。有没有其他方法可以在不需要敏感或受限作用域的文件夹中移动或创建文件 下面是函数Google apps script 在文件夹中创建电子表格文件而不使用auth/drive作用域,google-apps-script,google-drive-api,Google Apps Script,Google Drive Api,我以前问过一个关于创建文件并将其移动到文件夹的问题,但是现在,当我要发布这个简单的附加组件时,我遇到了移动文件所需的auth/drive范围的问题。auth/drive是一个敏感的作用域,我必须经过一个审查过程才能发布。我尝试将作用域更改为auth/drive.file,但显然这不适用于moveTo方法 为了在正确的位置创建一个文件,我不得不跳转,这让我感到沮丧。有没有其他方法可以在不需要敏感或受限作用域的文件夹中移动或创建文件 下面是函数 function createSpreadsheet(
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;
}
我相信你的目标如下
- 你想缩小谷歌应用程序脚本的范围。
- 您想使用
用于此https://www.googleapis.com/auth/drive.file
- 您想使用
https://www.googleapis.com/auth/drive必须使用
。因此,为了缩小范围,我建议使用驱动API而不是驱动服务
此修改脚本的流程如下所示
- 在这种情况下,
https://www.googleapis.com/auth/drive.metadata.readonly使用了
- 在这种情况下,
https://www.googleapis.com/auth/drive.file使用了
- 通过一次API调用,使用“文件:插入”方法创建电子表格并将其移动到特定文件夹
- 在这种情况下,
https://www.googleapis.com/auth/spreadsheets使用了
- 此示例脚本适用于您问题中的脚本。因此,当您使用方法来使用
在您的另一部分中,此脚本可能没有用处。请注意这一点。https://www.googleapis.com/auth/drive
- 从
和form.getTitle()
中,form.getId()
可能需要包括在内。请小心这个。而且,当您使用其他方法来使用其他作用域时,请包括它们https://www.googleapis.com/auth/forms
- 电子表格的创建范围为
,电子表格可由使用范围的应用程序使用。请小心这个https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.file
确实不在该列表中为受限范围,但根据您的应用程序,它可能符合敏感范围
为了检查这一点,您可以通过谷歌云控制台将范围添加到您的项目中,并查看是否会有一个锁定图标
但是,检索文件夹并移动文件将需要使用列表中的其他作用域,因此验证过程是必要的
核查过程也有例外情况;特别是对于您提到的附加组件,以下内容可能适用:
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);
}