Last active
August 2, 2018 23:54
-
-
Save mbierman/6efb84fda3a372e9df184769af6d2fb5 to your computer and use it in GitHub Desktop.
I use this to take CSV data about the temperature and fan speed of my Mac (https://gist.github.com/mbierman/af96ef30445f0359e2d91bb7e2c184ad). This Google App Script 1. converts CSV to a Google compatible format 2. copies the data 3. Renames the CSV for backup for a while... and then adds the data to the end of a spreadsheet which creates a chart.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function importData() { | |
| var fSource = DriveApp.getFolderById('enter ID Here'); // reports_folder_id = id of folder where csv reports are saved | |
| var fi = fSource.getFilesByName('report.csv'); // latest report file | |
| var ss = SpreadsheetApp.openById('enter ID here'); // data_sheet_id = id of spreadsheet that holds the data to be updated with new report data | |
| // Convert CSV > Google Sheet | |
| if ( fi.hasNext() ) { // proceed if "report.csv" file exists in the reports folder | |
| var file = fi.next(); | |
| // Logger.log('starting point: ' + file.getName()); | |
| Drive.Files.copy({}, file.getId(), {convert: true}); | |
| delteFile(file); | |
| } | |
| var fi = fSource.getFilesByName('report'); // converted report file from above | |
| if ( fi.hasNext() ) { // proceed if "report" file exists in the reports folder | |
| var file = fi.next(); | |
| // Logger.log('File : ' + file.getName()); | |
| var sss = SpreadsheetApp.open(file); // replace with source ID | |
| var ss = sss.getSheetByName('report'); // replace with source Sheet tab name | |
| var range = ss.getRange('A1:D30'); // assign the range you want to copy | |
| var data = range.getValues(); | |
| var tss = SpreadsheetApp.openById('ID here'); //replace with destination ID | |
| var ts = tss.getSheetByName('Sheet 1'); // replace with destination Sheet tab name | |
| ts.getRange(ts.getLastRow()+1, 1,30,4).setValues(data); //you will need to define the size of the copied data see getRange() | |
| // rename the report.csv file so it is not processed on next scheduled run | |
| file.setName("report-"+(new Date().toString())+".csv"); | |
| } | |
| }; | |
| function delteFile(myFileName) { | |
| var allFiles, idToDLET, myFolder, rtrnFromDLET, thisFile; | |
| myFolder = DriveApp.getFolderById('enter ID here'); | |
| allFiles = myFolder.getFilesByName(myFileName); | |
| while (allFiles.hasNext()) {//If there is another element in the iterator | |
| thisFile = allFiles.next(); | |
| idToDLET = thisFile.getId(); | |
| // Logger.log('idToDLET: ' + idToDLET); | |
| rtrnFromDLET = Drive.Files.remove(idToDLET); | |
| }; | |
| }; | |
| function DeleteOldFiles() { | |
| var Folders = new Array( | |
| '1x_iNzrNZDfgWz1MykMiADa_7Yd0kMtln' | |
| ); | |
| var Files; | |
| Logger.clear(); | |
| for each (var FolderID in Folders) { | |
| Folder = DriveApp.getFolderById(FolderID) | |
| Files = Folder.getFiles(); | |
| while (Files.hasNext()) { | |
| var File = Files.next(); | |
| if (new Date() - File.getDateCreated() > 5 * 24 * 60 * 60 * 1000) { | |
| File.setTrashed(true); // Places the file int the Trash folder | |
| //Drive.Files.remove(File.getId()); // Permanently deletes the file | |
| Logger.log('File ' + File.getName() + ' was deleted.'); | |
| } | |
| } | |
| } | |
| if(Logger.getLog() != '') | |
| MailApp.sendEmail('[email protected]', 'Backups have been removed from Google Drive', Logger.getLog()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment