I am working with Google Scripts Editor. The purpose of my script is to backup a spreadsheet from Google Sheets to my local as a csv. I would like each new backup to overwrite the previous backup file. Currently the script results in subsequent backup files as the same name but as a copy (Ex. filename (1).csv and filename (2).csv) Any help in overwriting would be appreciated.
function myFunction() {// UPDATE THE FOLDER ID for e.g. "My Drive > Docs > Backups"
var backupFolder = DriveApp.getFolderById("foldername");
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheetId = spreadsheet.getId();
var file = Drive.Files.get(spreadsheetId);
var url = file.exportLinks[MimeType.CSV];
// UPDATE THE SHEET-NAME TO BE EXPORTED - e.g. sheetName = "Malawi Ops"
// -- LEAVE IT BLANK TO EXPORT THE ENTIRE SPREADSHEET - i.e. sheetName = ""
var sheetName = "";
if (sheetName.length) {
var sheet = spreadsheet.getSheetByName(sheetName);
var sheetId = sheet.getSheetId();
url += "&gid=" + sheetId;
}
var token = ScriptApp.getOAuthToken();
var options = { headers: { Authorization: "Bearer " + token } };
var response = UrlFetchApp.fetch(url, options);
var doc = response.getBlob();
var backupDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH-mm-ss");
var backupName = spreadsheet.getName() + ".csv";
var tempFile = DriveApp.createFile(doc).setName(backupName);
tempFile.makeCopy(backupName, backupFolder);
tempFile.setTrashed(true);
}
The purpose of my script is to backup a spreadsheet from Google Sheets to my local as a csv.
. But in your script, the Google Spreadsheet is converted to an Excel file and it is put tobackupFolder
in your Google Drive. From this situation, I cannot understand about your goal. Can I ask you about the detail of it? – Tanaike