I have a source spreadsheet with content.
I have a destination template for that content.
I select and copy the content in the source spreadsheet.
I create a copy of the destination template and paste in the source spreadsheet content.
I then execute some code to export the destination template as an XLSX file and attach it to an email.
The email and the attachment come through, but the contents of the XLSX file match the original template - the content I pasted in is missing.
Yet if I take my export string and run it through the browser, it exports the XLSX file just fine with the contents!
It appears the export function is running before the content paste is complete and sending the newly created destination template without the contents.
I've already tried Utilities.sleep(30000), but no matter how long I wait, I always get a blank copy of the original template. WTF?!
Complete code:
function sendVendor() {
// Open the Active Spreadsheet
var ssMaster = SpreadsheetApp.getActiveSpreadsheet();
var sheetInsert = ssMaster.getSheetByName('Insert RFQ');
var rfqNumber = sheetInsert.getRange('AW2').getValue();
var sheetWorkUp = ssMaster.getSheetByName('WORK UP');
var backgroundColor = '#FF5050';
var row = 11;
var newSheetRow = 13;
var numRows = 0;
var valuesPartNumbers = sheetWorkUp.getRange(12, 2, 233, 1).getValues();
var size = valuesPartNumbers.filter(function(value) { return value != '' }).length;
size = size - 1;
// Create the new RFQ from Template
var template = DocsList.getFileById('1M2f5yoaYppx8TYO_MhctEhKM_5eW-QCxlmJdjWg9VUs'); // Quote Workup Template
var newRFQ = template.makeCopy('Vendor RFQ Request ' + rfqNumber);
var newRFQId = newRFQ.getId();
var folderNew = DocsList.getFolder('Vendor RFQ Requests');
newRFQ.addToFolder(folderNew)
// Open new RFQ
var ssTemplate = SpreadsheetApp.openById(newRFQId);
var sheetVendorRequest = ssTemplate.getSheetByName('Vendor Request');
var newTemplateURL = ssTemplate.getUrl();
var needPricing = new Array();
var valuesFRCosts = sheetWorkUp.getRange(12, 8, size, 1).getValues();
for (var i = 0; i < valuesFRCosts.length; i++) {
row++;
if (valuesFRCosts[i][0] == '') {
var sheetWorkUpRow = sheetWorkUp.getRange(row, 1, 1, sheetWorkUp.getLastColumn());
sheetWorkUpRow.setBackground(backgroundColor);
var sendToTemplate = sheetWorkUp.getRange(row, 1, 1, 6).getValues();
sendToTemplate[0].splice(2, 1);
sheetVendorRequest.getRange(newSheetRow, 2, 1, 5).setValues(sendToTemplate);
newSheetRow++;
}
}
var url = 'https://docs.google.com/feeds/download/spreadsheets/Export?key=' + newRFQId + '&exportFormat=xlsx';
var doc = UrlFetchApp.fetch(url);
var attachments = [{fileName:'Vendor RFQ Request ' + rfqNumber, content: doc.getContent(),mimeType:"application/vnd.ms-excel"}];
MailApp.sendEmail("user@domain.com", "Excel Export Test", "See the attached Excel file.", {attachments: attachments});
}
The solution is to add SpreadsheetApp.flush();
prior to the export (just before var url
). The SpreadsheetApp was still occupied with the template (in memory) after being copied - flushing it, and then exporting, forces the script to make a new call to the new Spreadsheet and get the latest data.
Thank you, Faustino Rodriguez.