【发布时间】:2020-10-31 22:56:05
【问题描述】:
我正在尝试在 Google Apps 脚本上创建一个自定义 pdf 网络应用程序,将值替换到我的模板 Google 文档中。我在 numGenerator 函数中生成数字,我也想返回这些数字以供以后在我的 python 文件中使用。
function numGenerator(digits){
let listOfNums = [];
const powerOfTen = Math.pow(10, digits);
//Addition
for (i=0; i<6; i++) {
let num = getRandomNumber(0, powerOfTen);
sendToList(num, listOfNums);
}
//Subtraction
for (i=6; i<14; i+=2) {
let num1 = getRandomNumber(0, powerOfTen);
let num2 = getRandomNumber(num1, powerOfTen);
sendToList(num2, listOfNums);
sendToList(num1, listOfNums);
}
//Multiplication
for (i=14; i<22; i+=2) {
let num1 = getRandomNumber(2, (powerOfTen/10));
let num2 = getRandomNumber(2, 20);
sendToList(num1, listOfNums);
sendToList(num2, listOfNums);
}
//Division
for (i=22; i<30; i+=2) {
let num2 = getRandomNumber(2, (powerOfTen/10));
let num1 = getRandomNumber(2, (num2/10));
sendToList(num1*num2, listOfNums);
sendToList(num2, listOfNums);
}
return listOfNums;
}
function createDocument(doc_id, digits) {
const listOfNums = numGenerator(digits)
var TEMPLATE_ID = '#########'; // Google Doc template
var documentId = DriveApp.getFileById(TEMPLATE_ID).makeCopy().getId();
drivedoc = DriveApp.getFileById(documentId);
drivedoc.setName("Mental Math Starter Exercise" + doc_id); // Copy created
doc = DocumentApp.openById(documentId);
var body = doc.getBody();
body.replaceText('<ID>', doc_id);
body.replaceText('<digits>', digits)
for (i=1; i<=30; i++){
body.replaceText('<num' + i + '>', listOfNums[i-1]) // Generated numbers inserted
}
drivedoc.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.EDIT);
return [listOfNums, "https://docs.google.com/document/d/" + documentId + "/export?format=pdf"]; // This might be a problem
}
function doGet(e) {
var invoice_id = e.parameter.invoice_id; // fetches parameters
var digits = e.parameter.digits // fetches parameters
var url = createDocument(invoice_id, digits); // url should be a list here
return (url[0], ContentService.createTextOutput(url[1])); //Should have returned the listOfNums and the document here
}
在我尝试从函数返回两个值之前,以下 Python 代码运行良好,下载了自定义工作表。
import requests
import random
from PyPDF2 import PdfFileMerger
import os
url = "https://script.google.com/macros/s/AKfycbyWXUQkpxyxDtj8lsOZtdaCBqRJUMZ1f9jXOFaTN82HtGIMIFc/exec?invoice_id={" \
"}&digits={}"
def createPDF(digits):
merger = PdfFileMerger()
for digit in digits:
id_num = random.randint(0, 10000)
print("processing ", id_num, digit)
docURL = requests.get(url.format(id_num, digit))
print("file generated")
document = requests.get(docURL.content)
print("file downloaded")
listOfNums = document.content[0]
with open("worksheet{}.pdf".format(id_num), "wb") as f:
f.write(document.content[1])
# merger.append(response.content)
merger.append("worksheet{}.pdf".format(id_num))
os.remove("worksheet{}.pdf".format(id_num))
merger.write("result.pdf")
merger.close()
print("Worksheet bundle downloaded")
但是,现在我尝试同时访问文档和 listOfNums,我收到以下错误:
File "/Users/vkhanna/PycharmProjects/MachineLearning/pdfRequest.py", line 22, in createPDF
f.write(document.content[1])
TypeError: a bytes-like object is required, not 'int'
如何成功返回 listOfNums 和文档?我知道我错过了一些简单的东西。
【问题讨论】:
-
您不能从 JavaScript 中的函数返回多个值,顺便说一句(除非在数组或对象中)。
ContentService.createTextOutput(url[1])因此永远不会返回。 -
@OlegValter 是的。但只返回最后一项。所以
url[0]永远不会返回。见comma operator -
另外
document.content是以“字节”为单位的响应。无法访问[0],[1],好像是数组或列表 -
@TheMaster - 我的错,我一直忘记逗号运算符实际上返回了一些东西......
-
@TheMaster - 不过我会调查一下 - 只是像往常一样需要一些时间
标签: javascript python google-apps-script web-applications