问题
I have a Google sheet that is exported as PDF and emailed using App Script but there are a few cases where the PDF shows #NAME? in cells where I used a custom function. But in most cases it is fully displayed.
Is there a way around this? It looks like before the sheet has fully refreshed, it is already exported into PDF. If I manually open the spreadsheet, it shows the figures but sometimes it refreshes and takes about 2 to 3 seconds to redisplay the values.
Here is how it looks like:
My code to email as PDF is here:
//Now, get the file and email as PDF attachment
var file = DriveApp.getFileById(GsheetFileID);
GmailApp.sendEmail(approverEmail, emailIdentifier + " OT Claim for your approval", "", {
cc: ccOthers,
htmlBody: html4Approver,
//attachments : [blob]
attachments : [file.getAs(MimeType.PDF)]
});
I have also tried exporting it this way but the result is still the same.
var url = "https://www.googleapis.com/drive/v3/files/" + fileID + "/export?mimeType=application/pdf";
var options = {
method: "GET",
headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions: true
};
var pdfFile = UrlFetchApp.fetch(url, options).getBlob();
Thanks in advance for any ideas.
回答1:
This is a common issue that arise then there's a lot of data and latency problems arise. You can workaround it using getDisplayValues() to create another sheet which will contain pure data.
This is sample code, which will load data correctly but w/o formatting
function copyTab(sheet) {
var data = SpreadsheetApp.getActive().getSheetByName(sheet).getDataRange().getDisplayValues();
if(data.length > 0) {
var bufferSheet = SpreadsheetApp.getActive().getSheetByName('Buffer');
if(!bufferSheet)
bufferSheet = SpreadsheetApp.getActive().insertSheet('Buffer');
else
bufferSheet.clear({contentsOnly: true});
bufferSheet.getRange(1,1,data.length,data[0].length).setValues(data);
/* Here you can add your code to create PDF from Buffer sheet */
}
}
来源:https://stackoverflow.com/questions/61755462/need-help-spreadsheet-formula-not-refreshed-in-exported-pdf