I need a google script which will automatically backup one particular web page every day in the Google Drive and save it as Spreadsheet (or xlsx file) if it's possible.
I found this script:
https://ctrlq.org/code/19687-backup-web-pages-daily-google-scripts
and using this:
http://stackoverflow.com/questions/36849344/save-web-page-as-google-doc-or-pdf-to-drive
manage to save it as pdf file.
I also try to use this mimeType conversion but I don't know how to import that part into the script.
http://stackoverflow.com/questions/22236558/is-there-a-way-to-save-html-text-to-a-file-in-google-drive-using-apps-script
I didn't manage to get xlsx files but pdf. I also don't know how to make folder structure in a way that backup files are saved only in main backup folder without creating new subfolders for every new date. Could someone please help me with this folder or xlsx problem, or both? I know very little about coding so for every help I would be grateful.
What I have tried:
Here is code which is working now and makes folder structure and saves as pdf:
var RESOURCE_URL = 'https://news.google.com',
BACKUP_FOLDER_ID = '<THE_ID_OF_THE_FOLDER_WHERE_YOUR_BACKUPS_WILL_LIVE>',
FOLDER_NAME_FORMAT = 'yyyy-MM-dd',
FILE_NAME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss",
FILE_EXT = '.pdf',
now = new Date(),
FOLDER_NAME = Utilities.formatDate(now, 'GMT', FOLDER_NAME_FORMAT),
FILE_NAME = Utilities.formatDate(now, 'GMT', FILE_NAME_FORMAT) + FILE_EXT;
function createBackup() {
var folder = getFolder(FOLDER_NAME);
createBackupFile(folder, FILE_NAME, fetchData());
}
function getFolder(name) {
var backupFolder = getBackupFolder(),
folders = backupFolder.getFoldersByName(name);
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = backupFolder.createFolder(name);
}
return folder;
}
function getBackupFolder() {
return DriveApp.getFolderById(BACKUP_FOLDER_ID);
}
function createBackupFile(folder, filename, data, overwrite) {
if (overwrite) {
var existingFiles = folder.getFilesByName(filename);
while (existingFiles.hasNext()) {
var file = existingFiles.next();
folder.removeFile(file);
}
}
}
function fetchData() {
var exportUrl = RESOURCE_URL;
var response = UrlFetchApp.fetch(exportUrl);
var htmlBody = response.getContentText();
var blob = Utilities.newBlob(htmlBody, 'text/html').getAs('application/pdf').setName(FILE_NAME);
return folder.createFile(blob);
}