0% found this document useful (0 votes)
5 views3 pages

Function MyFunction Edit

The document contains a Google Apps Script function that generates PDF reports from a Google Sheet. It retrieves data from a specific sheet, replaces placeholders in a template document with the data, and saves the PDF in a designated folder. Additionally, it sends an email with the generated PDF attached to the reporter's email address.

Uploaded by

SUPAT PUMJAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Function MyFunction Edit

The document contains a Google Apps Script function that generates PDF reports from a Google Sheet. It retrieves data from a specific sheet, replaces placeholders in a template document with the data, and saves the PDF in a designated folder. Additionally, it sends an email with the generated PDF attached to the reporter's email address.

Uploaded by

SUPAT PUMJAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

function myFunction() {

var docFile =
DriveApp.getFileById('1U6pKWTDu2LvJgRLgyvan8u_az8aEs_aD
9i92I65Ze4g')
var TempFolder =
DriveApp.getFolderById('1mW5Mp7MCjCG3VWVjxZ_e9axsujYYt
Rbi')
var PDFFolder = DriveApp.getFolderById('18psWxZWH0Y--
xnVB3KRb31P8pO6Yb3iC')

var ss =
SpreadsheetApp.openById('1s1EhndgfxkOG2rlpTpdZjCBK3Gxtq
rWXSIVOa-zXTu0')
var sh = ss.getSheetByName('การตอบแบบฟอร์ม 1')
var data = sh.getRange(ss.getLastRow(),1,1,20).getValues()
data.forEach(r=>{
var item1 = r[1]//Region
var item2 = r[2]//PEA Site ID
var item3 = r[3]//Item No
var item4 = r[4]//Location
var item5 = r[5]//Site Master
var item6 = r[6]//Status
var item7 = r[7]//Issue Date
var item8 = r[8]//Clear Date
var item9 = r[9]//Issue detail
var item10 = r[10]//Problem solving
var item11 = r[11]//Analysis results
var item12 = r[12]//Solution
var item13 = r[13]//Letter No
var item14 = r[14]//Remote Status
var item15 = r[15]//Remark
var item16 = r[16]//Reporter's name
var item17 = r[17]//Email
var item18 = r[18]//Attachment
var item19 = r[19]//ลำดับที่

CreatePDF(docFile,TempFolder,PDFFolder,item1,item2,item3,ite
m4,item5,item6,item7,item8,item9,item10,item11,item12,item
13,item14,item15,item16,item17,item18,item19)

})
}

function
CreatePDF(docFile,TempFolder,PDFFolder,item1,item2,item3,ite
m4,item5,item6,item7,item8,item9,item10,item11,item12,item
13,item14,item15,item16,item17,item18,item19){
var tempFile = docFile.makeCopy(TempFolder)
var tempDoc = DocumentApp.openById(tempFile.getId())
tempDoc.getBody().replaceText("Region",item1)
tempDoc.getBody().replaceText("PEA Site ID",item2)
tempDoc.getBody().replaceText("Item No",item3)
tempDoc.getBody().replaceText("Location",item4)
tempDoc.getBody().replaceText("Site Master",item5)
tempDoc.getBody().replaceText("Status",item6)
tempDoc.getBody().replaceText("Issue Date",item7)
tempDoc.getBody().replaceText("Clear Date",item8)
tempDoc.getBody().replaceText("Issue Detail",item9)
tempDoc.getBody().replaceText("Problem Solving",item10)
tempDoc.getBody().replaceText("Analysis Result",item11)
tempDoc.getBody().replaceText("Solution",item12)
tempDoc.getBody().replaceText("Letter No",item13)
tempDoc.getBody().replaceText("Remote",item14)
tempDoc.getBody().replaceText("Reporter name",item16)
tempDoc.getBody().replaceText("Email",item17)
tempDoc.getBody().replaceText("ลำดับที่",item19)
tempDoc.saveAndClose()
TempFolder.removeFile(tempFile)

var PdfContent = tempFile.getAs(MimeType.PDF)


var PdfFile =
PDFFolder.createFile(PdfContent).setName(item19)

MailApp.sendEmail(item17,'Service Report','Send Sevice Report


Detail',{
attachments: [PdfFile.getAs(MimeType.PDF)]
})
}

Short URL
https://forms.gle/pkeToj5ZxkN5nSsA6

You might also like