'Google Apps Script Automated PDF Download Problem (Mac vs Windows)
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 blobs = CreateReport();
const mimeTypes = { xlsx: MimeType.MICROSOFT_EXCEL, pdf: MimeType.PDF };
const blob_info = []
blobs.forEach(r=> blob_info.push([
{
data:
`data:${mimeTypes[type]};base64,` +
Utilities.base64Encode(r.getBytes()),
filename: `${r.getName()}`
}
]))
return blob_info;
}
function CreateReport(){
var ssMain = SpreadsheetApp.getActiveSpreadsheet();
const id = ssMain.getId()
const rawdata = Sheets.Spreadsheets.Values.get(id,'Input!A2:Z');
const linkdata = Sheets.Spreadsheets.Values.get(id,'Link!B1:B3');
const ss = ssMain.getActiveSheet();
const docid = ss.getRange("Link!B2").getValue();
const pdfid = ss.getRange("Link!B3").getValue();
const tempId = ss.getRange("Link!B1").getValue();
const folderdoc = DriveApp.getFolderById(docid);
const folderpdf = DriveApp.getFolderById(pdfid);
const templateId = DriveApp.getFileById(tempId);
const fileBlobs = [] // We will use it to store info of all blob
for(var i = 0; i < rawdata.values.length; i++){
var check = rawdata.values[i][11];
if(check !== "Done")
{
var Name = rawdata.values[i][0];
var newdoc = DriveApp.getFileById(tempId).makeCopy();
var documentId = newdoc.getId();
var doc = DocumentApp.openById(documentId);
DriveApp.getFileById(documentId).setName(Name);
var body = DocumentApp.openById(documentId).getBody();
body.replaceText('#1#', rawdata.values[i][1]);
body.replaceText('#2#', rawdata.values[i][2]);
body.replaceText('#3#', rawdata.values[i][3]);
body.replaceText('#4#', rawdata.values[i][4]);
body.replaceText('#5#', rawdata.values[i][5]);
body.replaceText('#6#', rawdata.values[i][6]);
body.replaceText('#7#', rawdata.values[i][7]);
body.replaceText('#8#', rawdata.values[i][8]);
body.replaceText('#9#', rawdata.values[i][9]);
body.replaceText('#10#', rawdata.values[i][10]);
folderdoc.addFile(DriveApp.getFileById(documentId));
doc.saveAndClose();
var docblob = doc.getBlob();
docblob.setContentType
docblob.setName(doc.getName() + ".pdf");
folderpdf.createFile(docblob);
ss.getRange(i+2, 12).setValue("Done");
SpreadsheetApp.flush();
fileBlobs.push(docblob)
}
}
return fileBlobs
}
<h1>Downloading</h1>
<p>Please Wait...</p>
<script>
google.script.run.withSuccessHandler(function(r){
for (var i = 0 ; i < r.length; i++)
{
var a = document.createElement("a");
document.body.appendChild(a);
a.download = r[i][0].filename;
a.href = r[i][0].data;
a.click();
}
google.script.host.close()
}).createDataUrl("pdf");
//
</script>
Many thanks
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Umair Mohammad |
