将数据从新创建的 PDF 文件发送到我的网站时出现问题
Problem sending data from a newly created PDF file to my website
我创建一个 PDF,然后将其信息发送到我的网站,以便将此 PDF 转换为图像文件并发布:
SpreadsheetApp.flush();
var theurl = 'https://docs.google.com/a/mydomain.org/spreadsheets/d/' +
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' +
'/export?format=pdf' +
'&size=0' +
'&portrait=true' +
'&fitw=true' +
'&top_margin=0' +
'&bottom_margin=0' +
'&left_margin=0' +
'&right_margin=0' +
'&sheetnames=false&printtitle=false' +
'&pagenum=false' +
'&gridlines=false' +
'&fzr=FALSE' +
'&gid=' +
'aaaaaaaaaaa';
var token = ScriptApp.getOAuthToken();
var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer ' + token } });
var pdfBlob = docurl.getBlob();
//...get token and Blob (do not create the file);
var fileName = ss.getSheetByName("General").getRange("H2").getValue();
//Access or create the 'Archives' folder;
var folder;
var folders = DriveApp.getFoldersByName("Archives");
if(folders.hasNext()) {
folder = folders.next();
}else {
folder = DriveApp.createFolder("Archives");
}
//Remove duplicate file with the same name;
var existing = folder.getFilesByName(fileName);
if(existing.hasNext()) {
var duplicate = existing.next();
if (duplicate.getOwner().getEmail() == Session.getActiveUser().getEmail()) {
var durl = 'https://www.googleapis.com/drive/v3/files/'+duplicate.getId();
var dres = UrlFetchApp.fetch(durl,{
method: 'delete',
muteHttpExceptions: true,
headers: {'Authorization': 'Bearer '+token}
});
var status = dres.getResponseCode();
if (status >=400) {
} else if (status == 204) {
folder.createFile(pdfBlob.setName(fileName));
}
}
} else {
folder.createFile(pdfBlob.setName(fileName));
}
Utilities.sleep(5000);
createPostByFileName(folder, fileName);
function createPostByFileName(folder, fileName) {
var fileIterator = folder.getFilesByName(fileName);
if(fileIterator.hasNext()) {
var file = fileIterator.next()
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW)
name = file.getName();
league = name.split(' ')[0];
title = name.split(league)[1].split('.pdf')[0];
link = file.getUrl();
shareable = link.split('/view')[0];
id = file.getId();
var data = {
'api_league_name': league,
'title': title,
'google_drive_id': id,
'google_drive_url': shareable,
'pass': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
};
var options = {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(data)
};
UrlFetchApp.fetch('https://www.xxx.com.br/api/posts', options);
}
}
出于某种原因,我不得不在调用将 PDF 数据发送到我的网站的函数之前放置 Utilities.sleep(5000);
,因为如果我不这样做,当网站尝试将 PDF 转换为图像时,出现问题,好像 PDF 在文件夹中尚不可用,或者在可以访问文件之前它会产生很大的减速。
显然这样 sleep
不是一个专业的破案方式,因为那样我什至不知道为什么会这样。
谁有过这样的经历,能告诉我应该怎么专业的进行吗?
从评论中的讨论来看,当Drive API 用于您的脚本时,它变成如下。在使用它之前,请在高级 Google 服务中启用驱动器 API。
修改后的脚本:
function sample() {
const createFile = (filename, blob, folderId) => Drive.Files.insert({ title: filename, parents: [{ id: folderId }] }, blob);
SpreadsheetApp.flush();
var theurl = 'https://docs.google.com/a/mydomain.org/spreadsheets/d/' +
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' +
'/export?format=pdf' +
'&size=0' +
'&portrait=true' +
'&fitw=true' +
'&top_margin=0' +
'&bottom_margin=0' +
'&left_margin=0' +
'&right_margin=0' +
'&sheetnames=false&printtitle=false' +
'&pagenum=false' +
'&gridlines=false' +
'&fzr=FALSE' +
'&gid=' +
'aaaaaaaaaaa';
var token = ScriptApp.getOAuthToken();
var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer ' + token } });
var pdfBlob = docurl.getBlob();
var fileName = ss.getSheetByName("General").getRange("H2").getValue();
var folder;
var folders = DriveApp.getFoldersByName("Archives");
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder("Archives");
}
var existing = folder.getFilesByName(fileName);
var pdfFileId = "";
if (existing.hasNext()) {
var duplicate = existing.next();
if (duplicate.getOwner().getEmail() == Session.getActiveUser().getEmail()) {
var durl = 'https://www.googleapis.com/drive/v3/files/' + duplicate.getId();
var dres = UrlFetchApp.fetch(durl, {
method: 'delete',
muteHttpExceptions: true,
headers: { 'Authorization': 'Bearer ' + token }
});
var status = dres.getResponseCode();
if (status >= 400) {
} else if (status == 204) {
var obj = createFile(fileName, pdfBlob, folder.getId());
pdfFileId = obj.id;
}
}
} else {
var obj = createFile(fileName, pdfBlob, folder.getId());
pdfFileId = obj.id;
}
// Utilities.sleep(5000);
Drive.Permissions.insert({ role: "reader", type: "anyone" }, pdfFileId);
league = fileName.split(' ')[0];
title = fileName.split(league)[1].split('.pdf')[0];
shareable = "https://drive.google.com/file/d/" + pdfFileId;
id = pdfFileId;
var data = {
'api_league_name': league,
'title': title,
'google_drive_id': id,
'google_drive_url': shareable,
'pass': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
};
var options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(data)
};
UrlFetchApp.fetch('https://www.xxx.com.br/api/posts', options);
}
注:
- 这是测试是否可以删除
Utilities.sleep(5000)
的示例脚本。
我创建一个 PDF,然后将其信息发送到我的网站,以便将此 PDF 转换为图像文件并发布:
SpreadsheetApp.flush();
var theurl = 'https://docs.google.com/a/mydomain.org/spreadsheets/d/' +
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' +
'/export?format=pdf' +
'&size=0' +
'&portrait=true' +
'&fitw=true' +
'&top_margin=0' +
'&bottom_margin=0' +
'&left_margin=0' +
'&right_margin=0' +
'&sheetnames=false&printtitle=false' +
'&pagenum=false' +
'&gridlines=false' +
'&fzr=FALSE' +
'&gid=' +
'aaaaaaaaaaa';
var token = ScriptApp.getOAuthToken();
var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer ' + token } });
var pdfBlob = docurl.getBlob();
//...get token and Blob (do not create the file);
var fileName = ss.getSheetByName("General").getRange("H2").getValue();
//Access or create the 'Archives' folder;
var folder;
var folders = DriveApp.getFoldersByName("Archives");
if(folders.hasNext()) {
folder = folders.next();
}else {
folder = DriveApp.createFolder("Archives");
}
//Remove duplicate file with the same name;
var existing = folder.getFilesByName(fileName);
if(existing.hasNext()) {
var duplicate = existing.next();
if (duplicate.getOwner().getEmail() == Session.getActiveUser().getEmail()) {
var durl = 'https://www.googleapis.com/drive/v3/files/'+duplicate.getId();
var dres = UrlFetchApp.fetch(durl,{
method: 'delete',
muteHttpExceptions: true,
headers: {'Authorization': 'Bearer '+token}
});
var status = dres.getResponseCode();
if (status >=400) {
} else if (status == 204) {
folder.createFile(pdfBlob.setName(fileName));
}
}
} else {
folder.createFile(pdfBlob.setName(fileName));
}
Utilities.sleep(5000);
createPostByFileName(folder, fileName);
function createPostByFileName(folder, fileName) {
var fileIterator = folder.getFilesByName(fileName);
if(fileIterator.hasNext()) {
var file = fileIterator.next()
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW)
name = file.getName();
league = name.split(' ')[0];
title = name.split(league)[1].split('.pdf')[0];
link = file.getUrl();
shareable = link.split('/view')[0];
id = file.getId();
var data = {
'api_league_name': league,
'title': title,
'google_drive_id': id,
'google_drive_url': shareable,
'pass': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
};
var options = {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(data)
};
UrlFetchApp.fetch('https://www.xxx.com.br/api/posts', options);
}
}
出于某种原因,我不得不在调用将 PDF 数据发送到我的网站的函数之前放置 Utilities.sleep(5000);
,因为如果我不这样做,当网站尝试将 PDF 转换为图像时,出现问题,好像 PDF 在文件夹中尚不可用,或者在可以访问文件之前它会产生很大的减速。
显然这样 sleep
不是一个专业的破案方式,因为那样我什至不知道为什么会这样。
谁有过这样的经历,能告诉我应该怎么专业的进行吗?
从评论中的讨论来看,当Drive API 用于您的脚本时,它变成如下。在使用它之前,请在高级 Google 服务中启用驱动器 API。
修改后的脚本:
function sample() {
const createFile = (filename, blob, folderId) => Drive.Files.insert({ title: filename, parents: [{ id: folderId }] }, blob);
SpreadsheetApp.flush();
var theurl = 'https://docs.google.com/a/mydomain.org/spreadsheets/d/' +
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' +
'/export?format=pdf' +
'&size=0' +
'&portrait=true' +
'&fitw=true' +
'&top_margin=0' +
'&bottom_margin=0' +
'&left_margin=0' +
'&right_margin=0' +
'&sheetnames=false&printtitle=false' +
'&pagenum=false' +
'&gridlines=false' +
'&fzr=FALSE' +
'&gid=' +
'aaaaaaaaaaa';
var token = ScriptApp.getOAuthToken();
var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer ' + token } });
var pdfBlob = docurl.getBlob();
var fileName = ss.getSheetByName("General").getRange("H2").getValue();
var folder;
var folders = DriveApp.getFoldersByName("Archives");
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder("Archives");
}
var existing = folder.getFilesByName(fileName);
var pdfFileId = "";
if (existing.hasNext()) {
var duplicate = existing.next();
if (duplicate.getOwner().getEmail() == Session.getActiveUser().getEmail()) {
var durl = 'https://www.googleapis.com/drive/v3/files/' + duplicate.getId();
var dres = UrlFetchApp.fetch(durl, {
method: 'delete',
muteHttpExceptions: true,
headers: { 'Authorization': 'Bearer ' + token }
});
var status = dres.getResponseCode();
if (status >= 400) {
} else if (status == 204) {
var obj = createFile(fileName, pdfBlob, folder.getId());
pdfFileId = obj.id;
}
}
} else {
var obj = createFile(fileName, pdfBlob, folder.getId());
pdfFileId = obj.id;
}
// Utilities.sleep(5000);
Drive.Permissions.insert({ role: "reader", type: "anyone" }, pdfFileId);
league = fileName.split(' ')[0];
title = fileName.split(league)[1].split('.pdf')[0];
shareable = "https://drive.google.com/file/d/" + pdfFileId;
id = pdfFileId;
var data = {
'api_league_name': league,
'title': title,
'google_drive_id': id,
'google_drive_url': shareable,
'pass': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
};
var options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(data)
};
UrlFetchApp.fetch('https://www.xxx.com.br/api/posts', options);
}
注:
- 这是测试是否可以删除
Utilities.sleep(5000)
的示例脚本。