通过Google Docs附加组件将文件从服务器下载到本地计算机 - 应用程序脚本
我需要修改我的应用程序脚本Google Docs附加项目,以将服务器中生成的文件(需要身份验证)下载到本地计算机上。没有找到将该文件直接下载到本地计算机的方法。因此,尝试先将其下载到驱动器中,然后将驱动器文件下载到本地计算机中。
我目前已经实施将该文件下载到Google Drive中,现在正在寻找将其下载到本地计算机的方法。搜索了使用驱动器API进行操作的方法。但还没有找到路。
下载文件将以低于格式。
.pdf,.epub,.jpg,.png,.zip,.mobi,.indd
-local-folder-form-form-google-apps-script“>文章,但仅用于下载文本内容。
这是我从服务器检索文件的代码,将其下载到驱动器中。
function downloadOutput(selectedFile){
var mimeType;
var template = new Template();
var documentProperties = PropertiesService.getDocumentProperties();
var jobFolderPath = replaceWhiteSpace(documentProperties.getProperty('JOB_FOLDER_PATH'));
var pathParts = jobFolderPath.split("/");
var outputFolderName = pathParts.pop();
var rootFolderID = getWriterRootFolder();
var filename = replaceWhiteSpace(selectedFile);
if (selectedFile.includes('.pdf')){
mimeType = 'application/pdf';
}
else if(selectedFile.includes('.epub')){
mimeType = 'application/epub+zip';
}
else if (selectedFile.includes('.jpg')){
mimeType = 'image/jpeg';
}
else if(selectedFile.includes('.png')){
mimeType = 'image/png';
}
else if(selectedFile.includes('.zip')){
mimeType= 'application/zip';
}
else if(selectedFile.includes('.mobi')){
mimeType= 'application/x-mobipocket-ebook';
}
else if(selectedFile.includes('.indd')){
mimeType = 'application/x-indesign';
}
//This is for retrieving the selectedFile from our server.
var response = sendRequest(template.server, template.customer, "/api/v2/","GET", "files" + jobFolderPath + "/"+ filename);
var output = {
title: selectedFile,
parents: [{'id':rootFolderID}],
mimeType: mimeType
};
var outputFile = Drive.Files.insert(output, response.getBlob());
//some tries to get a download from the drive file, but not worked.
var url = "https://www.googleapis.com/drive/v3/files/" + outputFile.id + "?alt=media&mimeType="+ mimeType;
var blob = UrlFetchApp.fetch(url, {
method: "get",
headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions: true
});
var rrr = UrlFetchApp.fetch(outputFile.downloadUrl,
{
method :"get",
headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken() }
});
}
请提出一种将驱动器文件下载到本地计算机中的“下载”文件夹或直接将服务器文件下载到本地计算机中的方法的方法,而无需将其保存在驱动器中。
提前致谢。
I need to modify my Apps Script Google Docs add-on project to download a file generated in our server (authentication needed) to the local machine. Didn't found a way to download that file directly into the local machine. So trying to download it first into the drive, then download the drive file into the local machine.
I have currently implemented to download that file into the Google Drive, now looking for a way to download it to the local machine. Searched for ways to do it with the Drive api. But didn't found a way yet.
The downloading file will be in below formats.
.pdf, .epub, .jpg, .png, .zip, .mobi, .indd
Found this article, but it is only for downloading text content.
This is my code for retrieving the file from our server, download it to the drive.
function downloadOutput(selectedFile){
var mimeType;
var template = new Template();
var documentProperties = PropertiesService.getDocumentProperties();
var jobFolderPath = replaceWhiteSpace(documentProperties.getProperty('JOB_FOLDER_PATH'));
var pathParts = jobFolderPath.split("/");
var outputFolderName = pathParts.pop();
var rootFolderID = getWriterRootFolder();
var filename = replaceWhiteSpace(selectedFile);
if (selectedFile.includes('.pdf')){
mimeType = 'application/pdf';
}
else if(selectedFile.includes('.epub')){
mimeType = 'application/epub+zip';
}
else if (selectedFile.includes('.jpg')){
mimeType = 'image/jpeg';
}
else if(selectedFile.includes('.png')){
mimeType = 'image/png';
}
else if(selectedFile.includes('.zip')){
mimeType= 'application/zip';
}
else if(selectedFile.includes('.mobi')){
mimeType= 'application/x-mobipocket-ebook';
}
else if(selectedFile.includes('.indd')){
mimeType = 'application/x-indesign';
}
//This is for retrieving the selectedFile from our server.
var response = sendRequest(template.server, template.customer, "/api/v2/","GET", "files" + jobFolderPath + "/"+ filename);
var output = {
title: selectedFile,
parents: [{'id':rootFolderID}],
mimeType: mimeType
};
var outputFile = Drive.Files.insert(output, response.getBlob());
//some tries to get a download from the drive file, but not worked.
var url = "https://www.googleapis.com/drive/v3/files/" + outputFile.id + "?alt=media&mimeType="+ mimeType;
var blob = UrlFetchApp.fetch(url, {
method: "get",
headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions: true
});
var rrr = UrlFetchApp.fetch(outputFile.downloadUrl,
{
method :"get",
headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken() }
});
}
Please suggest a way to download the drive file to the 'Downloads' folder in local machine or download the server file directly into the local machine without saving it in the drive.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要应用程序脚本执行服务器端,就没有直接的方法可以使用它下载到您的计算机上。
但是,当您将文件放在驱动器中时,使用客户端库 for Google API,在您的情况下 drive api 直接到您的机器?
步骤很容易,查找文件,抓住ID并下载到您的计算机上。
Google具有下载文件的特定指南。 “ https://developers.google.com/drive/guides/guides/manage-downloads#download_a_file_stored_on_google_google_drive” rel =“ nofollow noreferrer”>从/Develoverers.google.com/drive/api/guides/manage-downloads#download_a_document“ rel =” nofollow noreferrer'>下载Google Workspace文档,以及搜索特定的文件,通过
name = my_server_side_side_generated_generated_file
或mimeType ='application/epplact/epub +zip'
,或任何这是用于在node.js下载文件的简单用例,但是您也有有关Python和Java的示例。
文档:
As long as Apps Script is server side executed, there is no direct way to use it to download to your machine.
But, as you have your file is in your Drive, what about using one of the client libraries for Google APIs, in your case Drive API, to download it directly to your machine?
The steps are easy, look for the file, grab the ID and download to your machine.
Google has specific guides for Download files, this includes Download a general file from Google Drive and Download Google Workspace Document, and for search for a specific file, via
name = my_server_side_generated_file
ormimeType = 'application/epub+zip'
, or whatever query method you want to use.This is the simple use case for downloading a file in Node.js , but you have examples also for Python and Java.
Documentation: