0

I'm trying to save my results into PDF file after google sheet calculation. Here's my google sheet file: Google sheet(img)

After someone submit my form J Column sum all answers and gets 39. Then I want to convert my template file template(img) to PDF, so I wrote this code:

function afterFormSubmit(e) {   
  Logger.log(JSON.stringify(e));
    var formValues = e.namedValues;
  Logger.log(formValues);                               
        const   info = e.namedValues;
    createPDF(info);


}
                                                                                                    
function createPDF(info){                                                                                                   
                                                                                                    
const pdfFolder = DriveApp.getFolderById("1YPyX8gv3W-JXR0uovMdr");                                                                                                  
const tempFolder = DriveApp.getFolderById("1m9Xcc9hmhJjc83cSgCW26u");                                                                                                   
const templateDoc = DriveApp.getFileById("1r4m3-FtzMzlO_SmGMPuGraJhEIBsQA");                                                                                                    
                                                                                                    
const newTempFile = templateDoc.makeCopy(tempFolder);                                                                                                   
                                                                                                    
const openDoc = DocumentApp.openById(newTempFile.getId())                                                                                                   
const body = openDoc.getBody();                                                                                                 
body.replaceText("{T}", info['Timestamp'][0]);
body.replaceText("{A2}" ,info['answer2'][0]);
body.replaceText("{A3}" ,info['answer3'][0]);   
body.replaceText("{A4}" ,info['answer4'][0]);   
body.replaceText("{A5}" ,info['answer5'][0]);       
body.replaceText("{A6}" ,info['answer6'][0]);       
body.replaceText("{A7}" ,info['answer7'][0]);               
body.replaceText("{E}", info['Email'][0]);      
body.replaceText("{R}" ,info['Results'][0]);        //Please fix it: PDF Not showing/Saving Results                                                                                             
openDoc.saveAndClose();                                                                                                 
                                                                                                    
                                                                                                    
                                                                                                    
const blobPDF = newTempFile.getAs(MimeType.PDF);                                                                                                    
const pdfFile = pdfFolder.createFile(blobPDF).setName("Quiz finish time:"+" "+ info['Timestamp'][0]);                                                                                                   
tempFolder.removeFile(newTempFile); 


}   

But when this code converts my template file to PDF he don't show/save my result {R} PDF(img), but all other body.replaceText(); works well. Maybe someone know how to deal with that (how to save/show Result into PDF file)?

1
  • What have you done to try to debug the problem? Where are you losing the data? Commented Feb 7, 2021 at 17:56

1 Answer 1

0

I get response that my google app script code is only for google form responses... So my first code didn't works to get Results... But this one is what I'm looked for:

function afterFormSubmit() {
var ss= SpreadsheetApp.openById("YOUR_SHEET_ID").getSheetByName("SHEET_NAME");
ss.getLastRow();
var info=ss.getRange("A"+ss.getLastRow()+":J"+ss.getLastRow()).getValues()[0];
Logger.log(info);

  createPDF(info);


}
                                                                                                    
function createPDF(info){                                                                                                   
                                                                                                
                                                                                                    
const pdfFolder = DriveApp.getFolderById("FOLDER_ID");                                                                                                  
const tempFolder = DriveApp.getFolderById("FOLDER_ID");                                                                                                 
const templateDoc = DriveApp.getFileById("DOC_ID");                                                                                                 
                                                                                                    
const newTempFile = templateDoc.makeCopy(tempFolder);                                                                                                       
                                                                                                    
const openDoc = DocumentApp.openById(newTempFile.getId())                                                                                                   
const body = openDoc.getBody();     
var dt=Utilities.formatDate(info[0],"GMT"-5.00,"MM/dd/YYYY HH:mm:ss");
body.replaceText("{T}", dt);
body.replaceText("{A3}" ,info[2]);  
body.replaceText("{A4}" ,info[3]);  
body.replaceText("{A5}" ,info[4]);      
body.replaceText("{A6}" ,info[5]);      
body.replaceText("{A7}" ,info[6]);              
body.replaceText("{E}", info[7]);
body.replaceText("{A2}" ,info[8]);  
body.replaceText("{R}" ,info[9]);                                                                           
openDoc.saveAndClose();                                                                                                 
                                                                                                    
                                                                                                    
                                                                                                    
const blobPDF = newTempFile.getAs(MimeType.PDF);                                                                                                    
const pdfFile = pdfFolder.createFile(blobPDF).setName("Quiz finish time:"+" "+ dt);                                                                                                 
tempFolder.removeFile(newTempFile); 


}   

````
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.