使用Google高级云端硬盘服务,使用Apps脚本在文件夹中创建新文件 [英] Create new file in a folder with Apps Script using Google Advanced Drive service

查看:127
本文介绍了使用Google高级云端硬盘服务,使用Apps脚本在文件夹中创建新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有四种方法可以创建一个新文件:


  • DocsList - 在Main中显示为 DocsList 名单。内置到Apps脚本中。

  • DriveApp - 在主列表中显示为 Drive 。内置到Apps脚本中。

  • 云端硬盘API - 在主列表中也显示为云端硬盘。必须将 添加到Apps脚本中。

  • DocumentApp - 在主列表中显示为 Document 。内置,但只创建一个文档文件。



它们都被称为服务。云端硬盘API称为高级服务。那么,你应该使用哪一个?我不知道,这取决于。这个问题是关于 Drive API 高级服务。



我不想使用2或3个服务来完成工作。我想只使用其中的一个。但要决定使用哪一个,我需要知道所有这些功能和选项。如果最简单和最简单的一个可以做我想做的一切,那么我会使用它。



如果我可以用Drive API创建一个新文件,但是然后我需要使用DriveApp服务将我使用Drive API创建的文件移动到我需要的文件夹中,然后在该特定情况下使用Drive API毫无意义。



我可以从Google Apps脚本 .gs 代码在我的Google云端硬盘中创建一个新文件,但文件会写入主我的云端硬盘。我想直接将文件写入子文件夹。我现在的代码是:

  var fileNameSetA ='someFile.jpg'; 
var uploadedBlobA =使用文件选择器上传的图像;

var fileTestDrive = {
title:fileNameSetA,
mimeType:'image / jpeg'
};

fileTestDrive = Drive.Files.insert(fileTestDrive,uploadedBlobA);

尽管代码有效,但我不知道为什么语法是这样的,我找不到可以告诉我为什么的文档。我可以找到一个属性列表:



标题: mimeType:是可选属性作为请求正文的一部分。从示例中,可选属性显然被放在一个键:值配对对象中。所以,是语法:

  Drive.Files.insert(可选属性,内容); 

还有必需的查询参数:

uploadType - > media , multipart resumable



但我没有看到任何必需的 uploadType 参数在示例代码中的任何位置指定。所以,我不明白Google的文档。



Google文档插入



是否可以使用Google Apps for Linux中的Google Advanced Drive服务直接写入特定硬盘?文件?

Drive API 的文档 / strong>在此链接中找到:



INSERT的驱动API



有一个请求正文部分。 插入可选属性之一是父母[] 。括号[]表示可以指定父母名单。 parents [] 的文档声明了这一点:


收集包含此文件的父文件夹。设置这个
字段将把文件放到所有提供的文件夹中。在插入时,如果提供
没有文件夹,则该文件将被放置在默认的根
文件夹中。


所以,。 。 。使用云端硬盘API 中的插入。 。 。 。 CAN 直接将新文件写入子文件夹。这是可能的。



现在, Google Drive SDK HTTP请求的命名和语法与位于Apps脚本中。



调用 .gs 文件内的Drive API HTTP Request的语法是以下三种之一:


  • Drive.Files.insert(文件资源)

  • 驱动器。 Files.insert(FILE资源,BLOB mediaData)

  • Drive.Files.insert(文件资源,BLOB mediaData,OBJECT optionalArgs)


上述列表中显示的语法来自Apps Script代码编辑器内的自动完成下拉列表。如果您输入 Drive.Files。,则会出现一个可能的方法列表。我找不到在线文档中任何地方的语法信息。



那么,父母[] 可选属性的位置在哪里?那不是Blob,所以我们可以排除。它可以是 FILE资源 OBJECT optionalArgs optionalArgs 表示它是对象,但是 FILE资源实际上也是一个对象。在这些例子中, FILE资源被构造为key:value对象。



上传文件 - 高级云端硬盘服务 - Google文档


There are four ways to create a new file:

  • DocsList - Shown as DocsList in the Main List. Built in to Apps Script.
  • DriveApp - Shown as Drive in the Main List. Built in to Apps Script.
  • Drive API - Also shown as Drive in the Main List. Must be added to Apps Script.
  • DocumentApp - Shown as Document in the Main List. Built in, but only creates a document file.

They are all called services. Drive API is called an advanced service. So, which one should you use? I don't know, it depends. This question is about the Drive API Advanced Service.

I don't want to use 2 or 3 of the services to get the job done. I'd like to just use one of them. But to decide which one to use, I need to know the capabilities and options of all of them. If the simplest and easiest one to use will do everything I want, then I'll use that.

If I can create a new file with Drive API, but then I need to use the DriveApp service to move the file I created with Drive API, to the folder I want it in, then using Drive API in that particular situation is pointless.

I can create a new file in my Google Drive from a Google Apps Script .gs code, but the file gets written to the main 'My Drive'. I want to write the file directly to a sub-folder. My current code is:

var fileNameSetA = 'someFile.jpg';
var uploadedBlobA = an image uploaded with a file picker;

var fileTestDrive = {
  title: fileNameSetA,
  mimeType: 'image/jpeg'
};

fileTestDrive = Drive.Files.insert(fileTestDrive, uploadedBlobA);

Even though the code works, I have no idea why the syntax is the way it is, and I can't find documentation that tells me why. I can find a list of properties:

The title: and mimeType: are Optional Properties as part of the Request Body. From the example, the Optional Properties are obviously put in a key:value paired object. So, is the syntax:

Drive.Files.insert(optional properties, content);

There are also Required query parameters of:

uploadType --> media, multipart, resumable

But I don't see any required uploadType parameter designated anywhere in the example code. So, I don't understand Google's documentation.

Google Documentation Insert

Is it possible to write directly to a specific drive with Google Advanced Drive service in a Apps Script .gs code file? How do I do it?

解决方案

The documentation for INSERT for the Drive API is found at this link:

Drive API for INSERT

There is a section for Request body. One of the Optional Properties for Insert is parents[]. The brackets [] indicate that a list of parents can be designated. The documentation for parents[] states this:

Collection of parent folders which contain this file. Setting this field will put the file in all of the provided folders. On insert, if no folders are provided, the file will be placed in the default root folder.

So, . . . using Insert in Drive API, . . . . CAN write a new file directly to a subfolder. It's possible.

Now, the nomenclature and syntax for the Google Drive SDK, HTTP request is different than what is inside of Apps Script.

The syntax for invoking the Drive API HTTP Request inside of a .gs file is one of the following three:

  • Drive.Files.insert(FILE resource)
  • Drive.Files.insert(FILE resource, BLOB mediaData)
  • Drive.Files.insert(FILE resource, BLOB mediaData, OBJECT optionalArgs)

The syntax shown in the list above is from the auto-complete drop down list inside the Apps Script code editor. If you type Drive.Files. a list of possible methods will appear. I can't find information about the syntax anywhere in the online documentation.

So, where does the parents[] optional property go? Well, it's not a Blob, so we can rule that out. It's either FILE resource, or OBJECT optionalArgs. optionalArgs indicates that it's an object, but FILE resource is actually also an object.

In the examples, the FILE resource is constructed as key:value pair object.

Uploading Files - Advanced Drive Service - Google Documentation

这篇关于使用Google高级云端硬盘服务,使用Apps脚本在文件夹中创建新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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