如何使用高级驱动服务上传文件
How to Use Advanced Drive Service to Upload Files
我有以下 Google Apps 脚本,它从上传表单中获取一个文件并将其自动存储在我的 Google 驱动器中(下面代码段中的完整代码)。问题在于它的这一部分:
var file = folder.createFile(blob);
//Get root folder and pull all existing folders, plus setup variables pulled from form
var dropbox = form.Country;
var filename = form.reportType+".xls";
var rootfolder = DriveApp.getFolderById("0Byvtwn42HsoxfnVoSjB2NWprYnRiQ2VWUDZEendNOWwwM1FOZk1EVnJOU3BxQXhwU0pDSE0");
//Note root folder is Live Uploads Folder in Flatworld App folder structure
var folder, folders = rootfolder.getFoldersByName(dropbox);
//Check if folder exists and if not create
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = rootfolder.createFolder(dropbox);
}
//Check if file already exists and delete if it does
var file, files = folder.getFilesByName(filename);
while( files.hasNext()){
file = files.next();
file.setTrashed(true);
}
//Upload file and set various properties
var blob = form.myFile;
var file = folder.createFile(blob);
var timeStamp = new Date();
file.setDescription("Uploaded via BNI Upload Form by " + form.myName + " on: " + timeStamp);
//Set file name slightly differently for Weekly Member Report (do not want to overright based on name just keep each extract so add timestamp to name)
if (form.reportType == "Member Weekly"){
file.setName(form.reportType + timeStamp + ".xls");
}
else
{
file.setName(filename);
}
我正在使用 DriveApp class 的标准 Google 应用服务 createFile
方法,但它的文件上传大小很小,上传较大文件时速度非常慢关闭到这个限度。
我想转到高级 Google 服务,让您可以直接在 Google 应用程序中使用 Google 的 public API 方法脚本。我已经启用了驱动器 API 并认为我想使用这样的东西:
function uploadFile() {
var image = UrlFetchApp.fetch('http://goo.gl/nd7zjB').getBlob();
var file = {
title: 'google_logo.png',
mimeType: 'image/png'
};
file = Drive.Files.insert(file, image);
Logger.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
}
但我正在努力将它与我现有的代码一起实现,并且示例语言都是针对那些使用其他语言编码并使用 API 而不是仅使用 Apps 脚本编写的。
任何人都可以建议使用高级方法所需的代码修改,以允许更快和更大的上传吗?
在 Drive
和 DriveApp
之间有一些来回可能,而不是完全陷入纯粹的 Drive API。
//Upload file and set various properties
var mediaData = form.myFile;
var timeStamp = new Date();
var resource = {
title: (form.reportType == "Member Weekly") ? form.reportType + timeStamp + ".xls" : filename,
mimetype: 'application/vnd.ms-excel',
description: "Uploaded via BNI Upload Form by " + form.myName + " on: " + timeStamp
};
var file = Drive.Files.insert(resource, mediaData); // create file using Drive API
var fileId = file.id;
var DriveAppFile = DriveApp.getFileById(fileId); // retrieve file in DriveApp scope.
DriveApp.removeFile(DriveAppFile); // remove new file from Users root My Drive
folder.addFile(DriveAppFile); // puts file in selected folder
此代码中的 Drive
指的是从“资源”菜单启用的 Drive API Advanced Google Service。您还需要从开发人员控制台将其作为 API 启用。这些高级服务就像库一样附加到您的脚本中。 Drive
这是库的默认设置。
我有以下 Google Apps 脚本,它从上传表单中获取一个文件并将其自动存储在我的 Google 驱动器中(下面代码段中的完整代码)。问题在于它的这一部分:
var file = folder.createFile(blob);
//Get root folder and pull all existing folders, plus setup variables pulled from form
var dropbox = form.Country;
var filename = form.reportType+".xls";
var rootfolder = DriveApp.getFolderById("0Byvtwn42HsoxfnVoSjB2NWprYnRiQ2VWUDZEendNOWwwM1FOZk1EVnJOU3BxQXhwU0pDSE0");
//Note root folder is Live Uploads Folder in Flatworld App folder structure
var folder, folders = rootfolder.getFoldersByName(dropbox);
//Check if folder exists and if not create
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = rootfolder.createFolder(dropbox);
}
//Check if file already exists and delete if it does
var file, files = folder.getFilesByName(filename);
while( files.hasNext()){
file = files.next();
file.setTrashed(true);
}
//Upload file and set various properties
var blob = form.myFile;
var file = folder.createFile(blob);
var timeStamp = new Date();
file.setDescription("Uploaded via BNI Upload Form by " + form.myName + " on: " + timeStamp);
//Set file name slightly differently for Weekly Member Report (do not want to overright based on name just keep each extract so add timestamp to name)
if (form.reportType == "Member Weekly"){
file.setName(form.reportType + timeStamp + ".xls");
}
else
{
file.setName(filename);
}
我正在使用 DriveApp class 的标准 Google 应用服务 createFile
方法,但它的文件上传大小很小,上传较大文件时速度非常慢关闭到这个限度。
我想转到高级 Google 服务,让您可以直接在 Google 应用程序中使用 Google 的 public API 方法脚本。我已经启用了驱动器 API 并认为我想使用这样的东西:
function uploadFile() {
var image = UrlFetchApp.fetch('http://goo.gl/nd7zjB').getBlob();
var file = {
title: 'google_logo.png',
mimeType: 'image/png'
};
file = Drive.Files.insert(file, image);
Logger.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
}
但我正在努力将它与我现有的代码一起实现,并且示例语言都是针对那些使用其他语言编码并使用 API 而不是仅使用 Apps 脚本编写的。
任何人都可以建议使用高级方法所需的代码修改,以允许更快和更大的上传吗?
在 Drive
和 DriveApp
之间有一些来回可能,而不是完全陷入纯粹的 Drive API。
//Upload file and set various properties
var mediaData = form.myFile;
var timeStamp = new Date();
var resource = {
title: (form.reportType == "Member Weekly") ? form.reportType + timeStamp + ".xls" : filename,
mimetype: 'application/vnd.ms-excel',
description: "Uploaded via BNI Upload Form by " + form.myName + " on: " + timeStamp
};
var file = Drive.Files.insert(resource, mediaData); // create file using Drive API
var fileId = file.id;
var DriveAppFile = DriveApp.getFileById(fileId); // retrieve file in DriveApp scope.
DriveApp.removeFile(DriveAppFile); // remove new file from Users root My Drive
folder.addFile(DriveAppFile); // puts file in selected folder
此代码中的 Drive
指的是从“资源”菜单启用的 Drive API Advanced Google Service。您还需要从开发人员控制台将其作为 API 启用。这些高级服务就像库一样附加到您的脚本中。 Drive
这是库的默认设置。