简体   繁体   English

如何将命名的电子表格创建到文件夹中

[英]How to create named Spreadsheet into a folder

I can create a spreadsheet using the following我可以使用以下内容创建电子表格

fetch("https://sheets.googleapis.com/v4/spreadsheets?access_token=" + access_token, {
   method: 'POST', headers: { 'Accept': 'application/x-www-form-urlencoded' }
}).then(response => response.json()).then(json => {
   console.log(json);
});

What are the parameters for naming that sheet and assigning it to a folder?命名该工作表并将其分配给文件夹的参数是什么?

Unfortunately, in the current stage, the Spreadsheet cannot be created to the specific folder while the Spreadsheet can be created with your expected title.不幸的是,在当前阶段,无法将电子表格创建到特定文件夹,而可以使用您期望的标题创建电子表格。 In this case, Drive API is required to be used.在这种情况下,需要使用驱动器 API。 When you want to create a new Spreadsheet with your expected title in the specific folder using Drive API, how about the following modification?当您想使用驱动器 API 在特定文件夹中创建一个具有您预期标题的新电子表格时,如何进行以下修改?

In this case, please enable Drive API, and please include a scope of https://www.googleapis.com/auth/drive to your access token.在这种情况下,请启用驱动器 API,并请将https://www.googleapis.com/auth/drive的 scope 添加到您的访问令牌中。

Modified script 1:修改后的脚本 1:

const access_token = "###"; // Please set your access token.
const folderId = "###"; // Please set your folder ID.

fetch("https://www.googleapis.com/drive/v3/files", {
  method: 'POST', body: JSON.stringify({ name: "sample name", parents: [folderId], mimeType: "application/vnd.google-apps.spreadsheet" }), headers: { "Content-Type": "application/json", "Authorization": "Bearer " + access_token }
}).then(response => response.json()).then(json => {
  console.log(json);
});
  • When this script is run, a new Spreadsheet with the title of "sample name" is created to the specific folder of folderId .运行此脚本时,将在folderId的特定文件夹中创建一个标题为“sample name”的新电子表格。

Modified script 2:修改后的脚本 2:

If you are required to use Sheets API, how about the following modification?如果要求使用Sheets API,下面的修改怎么样?

const access_token = "###"; // Please set your access token.
const folderId = "###"; // Please set your folder ID.

fetch("https://sheets.googleapis.com/v4/spreadsheets", {
  method: 'POST', body: JSON.stringify({ properties: { title: "sample name" } }), headers: { "Content-Type": "application/json", "Authorization": "Bearer " + access_token }
}).then(response => response.json()).then(json => {
  console.log(json);
  fetch("https://www.googleapis.com/drive/v3/files/" + json.spreadsheetId + "?removeParents=root&addParents=" + folderId, {
    method: 'PATCH', body: JSON.stringify({}), headers: { "Content-Type": "application/json", "Authorization": "Bearer " + access_token }
  }).then(response => response.json()).then(json => {
    console.log(json);
  });
});
  • When this script is run, a new Spreadsheet with the title of "sample name" is created to the specific folder of folderId .运行此脚本时,将在folderId的特定文件夹中创建一个标题为“sample name”的新电子表格。

Note:笔记:

  • If you want to create a new Spreadsheet to the folder of the shared drive, please add a query parameter of supportsAllDrives=true to the endpoint of Drive API.如果要在共享驱动器的文件夹中创建一个新的电子表格,请在驱动器 API 的端点添加一个查询参数supportsAllDrives=true

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM