Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

240
Visualizações
¿Por qué esta función JS se ejecuta al hacer clic en un botón, pero no cuando se llama dentro de otra función?

La función que ejecuto principalmente savePo() guarda los datos de entrada de la página html en una hoja de cálculo y funciona bien. Si agrego savefile() al final de la función, veo que se ejecuta, pero no guarda el archivo pdf en el destino. Si ejecuto esta función directamente (haga clic en el botón), se ejecuta normalmente. ¿Qué me estoy perdiendo?

Función para guardar datos de formulario en una hoja de cálculo

 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();
}

Función que obtiene la página actual como pdf y la envía al lado del servidor

 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);
 });
}

Función para guardar el archivo en una carpeta de Google Drive

 function savePdf(data){
 const file = Utilities.newBlob(Utilities.base64Decode(data), MimeType.PDF).setName('fileName.pdf');
 DriveApp.getFolderById('folder_ID').createFile(file);
}
almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

En su secuencia de comandos, pensé que el motivo de su problema podría deberse a que savefile() se ejecuta con el proceso asíncrono. Por esto, google.script.host.close() se ejecuta antes de que se ejecute google.script.run.savePdf(btoa(p), filename) , y se produce su problema. Si mi comprensión es correcta, ¿qué tal la siguiente modificación?

Patrón 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);
 });
}

Patrón 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);
 });
}
almost 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda