Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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:
JavaScript
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",

// Customize your file extension.
FILE_EXT = '.pdf',

// Folder names are all going to look like this.
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());
}

// Ensures we're always working within the backup directory.
function getFolder(name) {
var backupFolder = getBackupFolder(),
    folders = backupFolder.getFoldersByName(name);

if (folders.hasNext()) {
    folder = folders.next();
} else {
    folder = backupFolder.createFolder(name);
}
return folder;
}

// Returns the root folder where our backups exist.
function getBackupFolder() {
return DriveApp.getFolderById(BACKUP_FOLDER_ID);
}

function createBackupFile(folder, filename, data, overwrite) {
if (overwrite) {
    // Technically we're not overwriting here. We're just deleting
    // the duplicates.
    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);
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900