I have an app script to export one of the Google sheets as Excel or CSV but I want it to overwrite existing exported file and not create a new file (It piles up on my downloads folder and I have apps that needs the file in same static file name.
I can't find an option to do this, so far the parameters of export URL doesn't have overwrite feature. Is this possible in App Script?
function ExprtMe()
{
var SheetApp = SpreadsheetApp.getActive();
ShtURL = SheetApp.getUrl();
ShtID = SheetApp.getId();
ShtGID = SheetApp.getSheetId();
var url = ShtURL.toString().replace("/edit", "/export?format=xlsx&gid=" ShtGID);
var html = HtmlService.createHtmlOutput('<html><script>'
'window.close = function(){window.setTimeout(function(){google.script.host.close()},9)};'
'var a = document.createElement("a"); a.href="' url '"; a.target="_blank";'
'if(document.createEvent){'
' var event=document.createEvent("MouseEvents");'
' if(navigator.userAgent.toLowerCase().indexOf("firefox")>-1){window.document.body.append(a)}'
' event.initEvent("click",true,true); a.dispatchEvent(event);'
'}else{ a.click() }'
'close();'
'</script>'
// Offer URL as clickable link in case above code fails.
'<body style="word-break:break-word;font-family:sans-serif;">Failed to open automatically. <a href="' url '" target="_blank" onclick="window.close()">Click here to proceed</a>.</body>'
'<script>google.script.host.setHeight(40);google.script.host.setWidth(410)</script>'
'</html>')
.setWidth( 90 ).setHeight( 1 );
SpreadsheetApp.getUi().showModalDialog( html, "Opening ..." );
}
CodePudding user response:
Unfortunately, overwriting an existing file in your local machine is not possible through App Script.
It is only possible to overwrite a file using App Script if the file is in your Drive by using the Drive API and update()
method.
I have two suggestions for you to be able to overwrite existing file while downloading in Google Chrome.
You can change chrome's setting for downloading file
- By default, Chrome will automatically save all files to the Downloads folder without asking the user for confirmation. There is a setting on chrome that prompt user download confirmation. You can do that by following these steps
Use third-party extensions
- There are several Chrome extension you can install if you want to overwrite existing files while downloading.