Here in the code I give inputs on Google Sheets, and Google Apps Script changes the parameters in Google Doc and saves the file as Google Doc. Finally, Script downloads Google Doc as pdf. However, script works fine only on Windows. On some macs; user needs to update the page before running the code, and for some macs; script creates Google Doc but fails to download as pdf
function openModalDialog() {
const html = HtmlService.createHtmlOutputFromFile("Download").setTitle("Succeed");
SpreadsheetApp.getUi().showSidebar(html);
}
function createDataUrl(type) {
type = "pdf";
const blob = CreateReport();
const mimeTypes = { xlsx: MimeType.MICROSOFT_EXCEL, pdf: MimeType.PDF };
return {
data:
`data:${mimeTypes[type]};base64,`
Utilities.base64Encode(blob.getBytes()),
filename: `${blob.getName()}.${type}`,
};
}
function CreateReport(){
var id = SpreadsheetApp.getActiveSpreadsheet().getId();
var rawdata = Sheets.Spreadsheets.Values.get(id,'Fill!B4:C17');
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var i = 1;
var Sirket = rawdata.values[1][i];
var Tur = rawdata.values[2][i];
var Sekil = rawdata.values[3][i];
if(Sirket == "Getir"){
if(Tur == "Belirsiz Süreli"){
if(Sekil == "Tam Zamanlı"){
var templateId = '##';
}
if(Sekil == "Yarı Zamanlı Günlük"){
var templateId = '##';
}
if(Sekil == "Yarı Zamanlı Saatlik"){
var templateId = '##';
}
}
if(Tur == "Belirli Süreli"){
if(Sekil == "Tam Zamanlı"){
var templateId = '##';
}
if(Sekil == "Yarı Zamanlı Günlük"){
var templateId = '##';
}
if(Sekil == "Yarı Zamanlı Saatlik"){
var templateId = '##';
}
}
}
if(Sirket == "Moov"){
if(Tur == "Belirsiz Süreli"){
if(Sekil == "Tam Zamanlı"){
var templateId = '##';
}
if(Sekil == "Yarı Zamanlı Günlük"){
var templateId = '##';
}
if(Sekil == "Yarı Zamanlı Saatlik"){
var templateId = '##';
}
}
if(Tur == "Belirli Süreli"){
if(Sekil == "Tam Zamanlı"){
var templateId = '##';
}
if(Sekil == "Yarı Zamanlı Günlük"){
var templateId = '##';
}
if(Sekil == "Yarı Zamanlı Saatlik"){
var templateId = '##';
}
}
}
if(Sirket == "Getirİş"){
if(Tur == "Belirsiz Süreli"){
if(Sekil == "Tam Zamanlı"){
var templateId = '##';
}
if(Sekil == "Yarı Zamanlı Günlük"){
var templateId = '##';
}
if(Sekil == "Yarı Zamanlı Saatlik"){
var templateId = '##';
}
}
if(Tur == "Belirli Süreli"){
if(Sekil == "Tam Zamanlı"){
var templateId = '##';
}
if(Sekil == "Yarı Zamanlı Günlük"){
var templateId = '##';
}
if(Sekil == "Yarı Zamanlı Saatlik"){
var templateId = '##';
}
}
}
var newdoc = DriveApp.getFileById(templateId).makeCopy();
var documentId = newdoc.getId();
var name = rawdata.values[4][i];
var doc = DocumentApp.openById(documentId);
DriveApp.getFileById(documentId).setName('Offer Letter of ' name);
var body = DocumentApp.openById(documentId).getBody();
body.replaceText('#SIRKET TELEFONU VE HATTI#', rawdata.values[9][i]);
body.replaceText('#SIRKET ARACI / ARAC ODENEGI#', rawdata.values[10][i]);
body.replaceText('#HAZIRLANMA TARIHI#', rawdata.values[0][i]);
body.replaceText('#AD SOYAD#', rawdata.values[4][i]);
body.replaceText('#UNVAN#', rawdata.values[5][i]);
body.replaceText('#DEPARTMAN#', rawdata.values[6][i]);
body.replaceText('#YONETICI AD SOYAD#', rawdata.values[7][i]);
body.replaceText('#UCRET#', rawdata.values[8][i]);
body.replaceText('#TEKLIF DONUS SON TARIHI#', rawdata.values[11][i]);
body.replaceText('#TEKLIFI ILETECEK KISI AD SOYAD#', rawdata.values[12][i]);
body.replaceText('#TEKLIFI ILETECEK KISI UNVANI#', rawdata.values[13][i]);
doc.saveAndClose();
var docblob = doc.getBlob();
docblob.setContentType
docblob.setName(doc.getName() ".pdf");
ss.getRangeList(['C4:C17']).setValue("");
SpreadsheetApp.flush();
return docblob;
}
<h1>İndiriliyor</h1>
<p>Lütfen bekleyiniz...</p>
<script>
google.script.run
.withSuccessHandler(({ data, filename }) => {
const a = document.createElement("a");
document.body.appendChild(a);
a.download = filename;
a.href = data;
a.click();
google.script.host.close();
})
.createDataUrl("pdf");
</script>
Many thanks
CodePudding user response:
I think you're returning the whole file content as blob data and that could be an issue.
Instead what you can do it that.
- Generate report in google doc (As is)
- Make is accessible to public
- Return download url of report (instead of blob data, as of now)
- Redirect user to download url
Something like this:
function CreateReport() {
// as is
var generatedReport = DriveApp.getFileById(documentId);
generatedReport.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);
return DriveApp.getFileById(documentId).getDownloadUrl();
}
function createDataUrl() {
var reportUrl = CreateReport();
return reportUrl;
}
Reference : https://developers.google.com/apps-script/reference/drive/file#getdownloadurl