The company I work at has been making and printing daily reports as .ods files. Historically it's been a painstaking process, so I'm trying to automate pieces of it with Google Sheets and Apps Script. The bookkeepers however still expect the identical .ods file every day.
I have a function that saves a copy of my Google sheet as .ods. It works fine for the most part, but there are some functions that don't quite work correctly so I need to strip them out and replace them with the values only before I save. I have code that looks like this:
function saveAsOds(){
let rangeWithFormulas = ss.getRangeByName("myRange");
rangeWithFormulas.copyTo(rangWithFormulas, {contentsOnly:true});
var urlExport = "https://docs.google.com/spreadsheets/d/" + ssId + "/export?format=ods&gid=" + sheetId;
var filename = Utilities.formatDate(date,"GMT-7","M-dd-yy")
// make sure copy function is finished before saving
Utilities.sleep(10000);
let blob = getFileAsBlob(urlExport);
blob.setName(filename)
let file = DriveApp.createFile(blob);
file.moveTo(targetFolder);
}
For some reason, this only works part of the time. I can confirm in my Google sheet that the copy function worked correctly and the range contains only values and no formulas, but about half the time the .ods file still ends up with formulas that don't work in it. I added the 10 second sleep, thinking that the blob was somehow being generated before the copy was done, but it doesn't seem to help. Any idea what's going on?