The function I'm primarily running savePo()
saves the html page input data into a spreadsheet and it works fine.
If I add savefile()
to the bottom of the function, I see that it gets run, but it doesn't save the pdf file to the destination.
If I run this function directly (button click), it runs normally. What am I missing?
Function saving form data to a spreadsheet
function savePo(user) {
//...gets html page input data and builds a 2D array, which gets saved in a Spreadsheet
google.script.run
.withFailureHandler(function (e) {
alert("The PO could not be saved./No se pudo guardar el pedido. " e);
})
.savePoData(poData, poOrigin);
savefile(); //How to run this function, which is detailed below
//Hides Order Po Header Field and its label
document.getElementById("selectOrderPo").style.display = "none";
document.getElementById("orderPoLabel").setAttribute("hidden", "hidden");
window.print();
google.script.host.close();
}
Function that gets current page as a pdf and sends to server-side
function savefile() {
var element = document.getElementById("pgBody");
var opt = {
margin: [2, 5, 2, 5], //top, left, bottom, right
filename: supplier " - " poNumber ".pdf",
image: {
type: "jpeg",
quality: 0.98,
},
html2canvas: {
scale: 2,
},
jsPDF: {
unit: "mm",
orientation: "landscape",
},
};
const base64File = html2pdf()
.set(opt)
.from(element)
.outputPdf()
.then(function (p) {
google.script.run.savePdf(btoa(p), filename);
});
}
Function saving the file to a Google Drive Folder
function savePdf(data){
const file = Utilities.newBlob(Utilities.base64Decode(data), MimeType.PDF).setName('fileName.pdf');
DriveApp.getFolderById('folder_ID').createFile(file);
}
CodePudding user response:
In your script, I thought that the reason for your issue might be due to that savefile()
is run with the asynchronous process. By this, google.script.host.close()
is run before google.script.run.savePdf(btoa(p), filename)
is run, and your issue occurs. If my understanding is correct, how about the following modification?
Pattern 1:
function savePo(user) {
//...gets html page input data and builds a 2D array, which gets saved in a Spreadsheet
google.script.run
.withFailureHandler(function (e) {
alert("The PO could not be saved./No se pudo guardar el pedido. " e);
})
.savePoData(poData, poOrigin);
savefile(); //How to run this function, which is detailed below
//Hides Order Po Header Field and its label
// document.getElementById("selectOrderPo").style.display = "none";
// document.getElementById("orderPoLabel").setAttribute("hidden", "hidden");
// window.print();
// google.script.host.close();
}
function savefile() {
var element = document.getElementById("pgBody");
var opt = {
margin: [2, 5, 2, 5], //top, left, bottom, right
filename: supplier " - " poNumber ".pdf",
image: {
type: "jpeg",
quality: 0.98,
},
html2canvas: {
scale: 2,
},
jsPDF: {
unit: "mm",
orientation: "landscape",
},
};
const base64File = html2pdf()
.set(opt)
.from(element)
.outputPdf()
.then(function (p) {
google.script.run.withFailureHandler(function () {
document.getElementById("selectOrderPo").style.display = "none";
document.getElementById("orderPoLabel").setAttribute("hidden", "hidden");
window.print();
google.script.host.close();
}).savePdf(btoa(p), filename);
});
}
Pattern 2:
function savePo(user) {
//...gets html page input data and builds a 2D array, which gets saved in a Spreadsheet
google.script.run
.withFailureHandler(function (e) {
alert("The PO could not be saved./No se pudo guardar el pedido. " e);
savefile(); //How to run this function, which is detailed below
})
.savePoData(poData, poOrigin);
}
function savefile() {
var element = document.getElementById("pgBody");
var opt = {
margin: [2, 5, 2, 5], //top, left, bottom, right
filename: supplier " - " poNumber ".pdf",
image: {
type: "jpeg",
quality: 0.98,
},
html2canvas: {
scale: 2,
},
jsPDF: {
unit: "mm",
orientation: "landscape",
},
};
const base64File = html2pdf()
.set(opt)
.from(element)
.outputPdf()
.then(function (p) {
google.script.run.withFailureHandler(function () {
document.getElementById("selectOrderPo").style.display = "none";
document.getElementById("orderPoLabel").setAttribute("hidden", "hidden");
window.print();
google.script.host.close();
}).savePdf(btoa(p), filename);
});
}