Automatically unzipping files on google drive with google app script - zip

I found and have made several codes to automatically unzip new files on my google drive with google app script, but none of them work. Can someone help me figure out how to get one code to work? The code is supposed to get a zip file from a given google drive folder and unzip it, so that the files within can be extracted and used in a different code.
Here are the six codes that might work, but don't. I only need one to work so I don't mind which one :)
Thank you in advance for your suggestions and help!
function unZip() {
var theFolderID = DriveApp.getFolderById(folderID);
var theFile = theFolderID.getFilesByType(MimeType.ZIP);
if (theFile.length > 0) {
for (var i in theFile) {
file = theFile[i];
var unZippedfile = Utilities.unzip(file);
var newfile = DriveApp.createFile(unZippedfile);
var fileId = SpreadsheetApp.create(newfile).getId();
var file = DriveApp.getFileById(fileId);
folderName.addFile(newfile);
root.removeFile(newfile);
}
}
}
function unZipItAll() {
var theFolder = getFolder_(folderName);
var theFile = theFolder.getFilesByType("application/zip");
if (theFile.length > 0) {
for (var i in theFile) {
file = theFile[i];
var unZippedfile = Utilities.unzip(file);
var newfile = DriveApp.createFile(unZippedfile);
var fileId = SpreadsheetApp.create(newfile).getId();
var file = DriveApp.getFileById(fileId);
folderName.addFile(newfile);
root.removeFile(newfile);
}
}
}
function unzip() {
var theFolderID = DriveApp.getFolderById(folderID);
var theFile = theFolderID.getFilesByType("application/zip");
while (theFile.hasNext()){
var fileBlob = theFile.next().getBlob();
fileBlob.setContentType("application/zip");
var unZippedfile = Utilities.unzip(fileBlob);
var newfile = DriveApp.createFile(unZippedfile);
var fileId = SpreadsheetApp.create(newfile).getId();
var file = DriveApp.getFileById(fileId);
folderName.addFile(file);
root.removeFile(newfile);
}
}
function unZipIt() {
var theFolder = DriveApp.getFolderById(folderID);
var theFile = theFolder.getFilesByType("application/zip");
var fileBlob = theFile.next().getBlob();
fileBlob.setContentType("application/zip");
var unZippedfile = Utilities.unzip(fileBlob);
var newDriveFile = DriveApp.createFile(unZippedfile[0]);
Logger.log(newDriveFile.getId())
}
function Unzip() {
var SourceFolder = DriveApp.getFolderById(folderID)
var DestinationFolder = DriveApp.getFolderById(folderID)
var ZIPFiles = SourceFolder.getFilesByType(MimeType.ZIP)
while (ZIPFiles.hasNext()){
var fileBlob = ZIPFiles.next().getBlob();
var unZippedfile = Utilities.unzip(fileBlob);
var newDriveFile = DestinationFolder.createFile(unZippedfile[0]);
}
}
function testzip(){
var theFolderID = DriveApp.getFolderById(folderID);
var theFile = theFolderID.getFilesByType("application/zip");
if (theFile.length > 0) {
for (var i in theFile) {
file = theFile[i];
var unZippedfile = Utilities.unzip(file);
var unzipstr = unZippedfile[0].getDataAsString();
var newfile = DriveApp.createFile(unzipstr);
folderName.addFile(newfile);
root.removeFile(newfile);
}
}
}

Related

what am i missing in this simple readline(nodeJS) piece of code?

Well, have no clue what am I missing.
I got a file with lines like that (time series you can say):
fs1,disk1,<date>,<value>
fs1,disk1,<date>,<value>
fs2,disk2,<date>,<value>
fs3,disk3,<date>,<value>
fs3,disk3,<date>,<value>
fs3,disk3,<date>,<value>
fs4,disk4,<date>,<value>
The lines are sorted (fs,disk,date)
so I want to read this file line by line with nodeJS readline, and as long as i have the same fs,disk to concatante this date,value series into a string and then to store that sting inside the dictionary..
for some silly reason that I miss, its not doing so.
here is the code:
var express = require('express');
var app = express();
var path = require('path');
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream(__dirname + '/disksTimeSeries/timeSeries')
});
app.use(express.static('public'))
app.get('/', function(req, res) {
res.send(index.html);
});
app.listen(3005);
var diskToTimeSeries = {};
var currDisk;
var prevDisk;
var currStr = "";
var seriesStr="";
var i = 0;
var disk;
var splitArr;
var data = {};
data['prevDisk'] = "";
data['seriesStr'] = "";
lineReader.on('line', function (line) {
console.log(line);
splitArr = line.split(",");
disk = splitArr[0] + "," + splitArr[1];
if (data['prevDisk'] && disk !== data['prevDisk'])
{
diskToTimeSeries[data['prevDisk']] = data['seriesStr'];
data['seriesStr'] = "";
}
else
{
data['prevDisk'] = disk;
data['seriesStr'] = data['seriesStr'] + (splitArr[2] + "," + splitArr[3] + "\n");
}
console.log(disk);
});
// viewed at http://localhost:8080
app.get('/disks', function(req, res) {
var diskIds = req.query.ids.split(":");
var resultStr = "";
diskIds.forEach(function(diskName){
resultStr += "disk name--" + diskName + "\n" + diskToTimeSeries[diskName];
});
res.send(resultStr);
});
module.exports = app;
What am I missing :( ?

Exporting a Google Sheet into an Excel using Google script

I am trying to automate a backup of a google sheet into an excel.
After trying different scripts seen here and there on Stackoverflow, the one I have is now running but nothing is happening.
Any ideas?
Here is the code:
function exportAsxlsx() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheetId = spreadsheet.getId()
var file = DriveApp.getFileById(spreadsheetId)
var url = 'https://docs.google.com/spreadsheets/d/'+spreadsheetId+'/export?format=xlsx';
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
var blobs = response.getBlob();
var folder = DriveApp.getFoldersByName('Exports');
if(folder.hasNext()) {
var existingPlan1 = DriveApp.getFilesByName('newfile.xlsx');
if(existingPlan1.hasNext()){
var existingPlan2 = existingPlan1.next();
var existingPlanID = existingPlan2.getId();
Drive.Files.remove(existingPlanID);
}
} else {
folder = DriveApp.createFolder('BackUp');
}
folder = DriveApp.getFoldersByName('BackUp').next();
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH:mm:ss");
var name = SpreadsheetApp.getActiveSpreadsheet().getName() + " Copy " + formattedDate;
folder.createFile(blobs).setName(name + '.xlsx')
}
So, here is a script that works:
function exportAsxlsx() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheetId = spreadsheet.getId()
var file = DriveApp.getFileById(spreadsheetId)
var url = 'https://docs.google.com/spreadsheets/d/'+spreadsheetId+'/export?format=xlsx';
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
var blobs = response.getBlob();
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH:mm:ss");
var name = SpreadsheetApp.getActiveSpreadsheet().getName() + " Copy " + formattedDate;
var folder = DriveApp.getFolderById("your folder ID");
folder.createFile(blobs).setName(name + '.xlsx');
}

Listing folders in specified folder

I'm trying this code to list the files inside cabinet's folder in NetSuite, but I can list only the files. I need to list the folders inside the folder with the ID: 28
The code I'm using:
var folder = 28;
var filters = new Array();
filters[0] = new nlobjSearchFilter('internalid', null, 'is', folder);
var columns = new Array();
var filename = new nlobjSearchColumn('name', 'file');
var fileid = new nlobjSearchColumn('internalid', 'file');
columns[0] = filename;
columns[1] = fileid;
var searchResult = nlapiSearchRecord('folder', null , filters , columns);
if(searchResult) {
for (var i = 0 ; i < searchResult.length; i++) {
alert(searchResult[i].getValue(filename)+searchResult[i].getValue(fileid));
};
};
The result:
warning File1.txt1938 11/22/2016 17:30:08.693
warning File2.txt1636 11/22/2016 17:30:08.693
warning File3.txt1939 11/22/2016 17:30:08.693
warning File4.txt4601 11/22/2016 17:30:08.693
this is correct, but it's listing only files, not folders?
Thanks!
Perform a UserEvent BeforeLoad function. All files have a parent folder ID.
// pass in the internal id of the file's parent folder
var folder = 415 // the folder ID we care about
var filters = new Array();
filters[0] = new nlobjSearchFilter('internalid', null, 'is', folder);
//file name and file internal ID
var columns = new Array();
var filename = new nlobjSearchColumn('name', 'file');
var fileid = new nlobjSearchColumn('internalid', 'file');
columns[0] = filename;
columns[1] = fileid;
// perform the search and loop through the findings
var searchResult = nlapiSearchRecord('folder', null , filters , columns);
if(searchResult) {
for (var i = 0 ; i < searchResult.length; i++) {
var f = searchResult[i];
//do what you want with f here
};
};
Credit: http://blog.prolecto.com/2014/01/09/get-netsuite-list-of-files-from-folder/
I was able to list the subfolders of folder with this code:
var folder = 3232;
var filters = new Array();
filters[0] = new nlobjSearchFilter('parent', null, 'is', folder);
var columns = new Array();
var filename = new nlobjSearchColumn('name');
var fileid = new nlobjSearchColumn('internalid');
columns[0] = filename;
columns[1] = fileid;
var searchResult = nlapiSearchRecord('folder', null , filters , columns);
if(searchResult) {
for (var i = 0 ; i < searchResult.length; i++) {
alert(searchResult[i].getValue(filename)+searchResult[i].getValue(fileid));
};
};

Google Apps Script: Save Spreadsheet as ODS for Local Backup

I could use a hand. My company uses Google Sheets extensively, and we need a way to access files when we lose our Internet connection.
I could not get any of the examples found on this site to work for creating xls or ods from Google Sheets via script.
I did script a way to create csv backups, accessible from a local Google Drive folder. When used with an hourly trigger, this script creates csv files of every sheet of any spreadsheet modified in the last hour, puts them in a folder, and zips it inside a folder specifically for backups. From there I can move the zip to our local server.
function backUpMaker() {
var backupFolderId = '<Id of Backup Folder>';
var timeNow = new Date();
var newFolder = DocsList.createFolder(timeNow);
var newFolderId =newFolder.getId();
newFolder.addToFolder(DocsList.getFolderById(backupFolderId));
newFolder.removeFromFolder(DocsList.getRootFolder());
var sheets = DriveApp.getFilesByType(MimeType.GOOGLE_SHEETS);
while (sheets.hasNext()) {
var sheet = sheets.next();
var lastUpdate = sheet.getLastUpdated();
var timeHourAgo = new Date();
timeHourAgo = timeNow - 3600000;
if(lastUpdate >= timeHourAgo){
var sheetId = sheet.getId();
var csv = eachSheet(sheetId,newFolderId);
}
}
var backupFolder = DocsList.getFolderById(backupFolderId);
try{
backupFolder.createFile(Utilities.zip(newFolder.getFiles(), timeNow + '.zip'));
} catch(err) {
Logger.log(err);
}
newFolder.setTrashed(true);
}
function eachSheet(key,newFolderId) {
var ss = SpreadsheetApp.openById(key);
var ssId = ss.getId();
var ssName = ss.getName();
var howManySheets = ss.getNumSheets();
try{
for (var sheetIndex=0; sheetIndex < howManySheets; sheetIndex++) {
var activeSheet = ss.getSheets()[sheetIndex];
var activeName = activeSheet.getName();
if(activeName != 'Dropdowns'){ //Skip a hidden sheet used for validation on many of our spreadsheets
var activeId = activeSheet.getSheetId();
var time = new Date();
var fileName = time + " Backup: " + ssName + " " + activeName + ".csv";
var csv = contentCSV(ssId,activeId);
var folder = DocsList.getFolderById(newFolderId);
folder.createFile(fileName, csv, 'text/plain');
}
}
} catch(err) {
Logger.log(err)
}
}
function contentCSV(key,gid) {
var file = DocsList.getFileById(key);
var response = UrlFetchApp.fetch("https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=" + key +
"&gid=" + gid + "&exportFormat=csv", oAuth());
var fileText = response.getContentText();
return fileText;
}
function oAuth() {
var oauthConfig = UrlFetchApp.addOAuthService("spreadsheets");
var scope = "https://spreadsheets.google.com/feeds"
oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oauthConfig.setAuthorizationUrl("https://accounts.google.com/OAuthAuthorizeToken");
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
var requestData = {
"oAuthServiceName": "spreadsheets",
"oAuthUseToken": "always",
};
return requestData;
}
How would I modify this to save as ods rather than csv? Or is there a better way to save backups? Thank you for your help!
Working code follows, thanks to input by #serge-insas :
Note: To make this work, enter your backup folder's id key, then choose ods or xlsx by commenting out the other choice. Then you need to set up a timed hourly trigger for backUpMaker(). Logs will catch errors for files that have been moved temporarily. Here goes:
function backUpMaker() {
var backupFolderId = '0B5--------------------1ZX1k';
//var exportFormat = 'ods';
var exportFormat = 'xlsx';
var timeNow = new Date();
var newFolder = DocsList.createFolder(timeNow);
var newFolderId =newFolder.getId();
newFolder.addToFolder(DocsList.getFolderById(backupFolderId));
newFolder.removeFromFolder(DocsList.getRootFolder());
var sheets = DriveApp.getFilesByType(MimeType.GOOGLE_SHEETS);
while (sheets.hasNext()) {
var sheet = sheets.next();
var lastUpdate = sheet.getLastUpdated();
var timeHourAgo = new Date();
timeHourAgo = timeNow - 3600000;
if(lastUpdate >= timeHourAgo){
try{
var key = sheet.getId();
var name = sheet.getName();
var fileName = timeNow + " Backup: " + name + "." + exportFormat;
var blob = contentBackup(key,exportFormat);
var folder = DocsList.getFolderById(newFolderId);
folder.createFile(blob).rename(fileName);
} catch(err){
Logger.log(err);
}
}
}
var backupFolder = DocsList.getFolderById(backupFolderId);
try{
backupFolder.createFile(Utilities.zip(newFolder.getFiles(), timeNow + '.zip'));
} catch(err) {
Logger.log(err);
}
newFolder.setTrashed(true);
}
function contentBackup(key,exportFormat) {
try{
if(exportFormat == 'xlsx'){
exportFormat = 'xls';
}
var file = DocsList.getFileById(key);
var response = UrlFetchApp.fetch("https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=" + key +
"&exportFormat=" + exportFormat, oAuth());
var fileBlob = response.getBlob();
return fileBlob;
} catch(err) {
Logger.log(err)
}
}
function oAuth() {
var oauthConfig = UrlFetchApp.addOAuthService("spreadsheets");
var scope = "https://spreadsheets.google.com/feeds"
oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oauthConfig.setAuthorizationUrl("https://accounts.google.com/OAuthAuthorizeToken");
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
var requestData = {
"oAuthServiceName": "spreadsheets",
"oAuthUseToken": "always",
};
return requestData;
}
Your code needed very few changes to get ods files.
2 differences : the requested format in the urlFetch becomes "ods" and the returned object is not a string anymore but a blob.
Beside that, I kept all the sheet selection functions that you wrote and it seems to work fine as well. (nice job you did !).
Below is the full code I reproduced from yours and changed as described. My tests were working fine.
function backUpMaker() {
var backupFolderId = '0B3qS--------------TXdlY2M';
var timeNow = new Date();
var newFolder = DocsList.createFolder(timeNow);
var newFolderId =newFolder.getId();
newFolder.addToFolder(DocsList.getFolderById(backupFolderId));
newFolder.removeFromFolder(DocsList.getRootFolder());
var sheets = DriveApp.getFilesByType(MimeType.GOOGLE_SHEETS);
while (sheets.hasNext()) {
var sheet = sheets.next();
var lastUpdate = sheet.getLastUpdated();
var timeHourAgo = new Date();
timeHourAgo = timeNow - 3600000;
if(lastUpdate >= timeHourAgo){
var sheetId = sheet.getId();
var csv = eachSheet(sheetId,newFolderId);
}
}
var backupFolder = DocsList.getFolderById(backupFolderId);
try{
backupFolder.createFile(Utilities.zip(newFolder.getFiles(), timeNow + '.zip'));
} catch(err) {
Logger.log(err);
}
newFolder.setTrashed(true);
}
function eachSheet(key,newFolderId) {
var ss = SpreadsheetApp.openById(key);
var ssId = ss.getId();
var ssName = ss.getName();
var howManySheets = ss.getNumSheets();
try{
for (var sheetIndex=0; sheetIndex < howManySheets; sheetIndex++) {
var activeSheet = ss.getSheets()[sheetIndex];
var activeName = activeSheet.getName();
if(activeName != 'Dropdowns'){ //Skip a hidden sheet used for validation on many of our spreadsheets
var activeId = activeSheet.getSheetId();
var time = new Date();
var fileName = time + " Backup: " + ssName + " " + activeName + ".ods";
var blob = contentODS(ssId,activeId);
var folder = DocsList.getFolderById(newFolderId);
folder.createFile(blob).rename(fileName);
}
}
} catch(err) {
Logger.log(err)
}
}
function contentODS(key,gid) {
var file = DocsList.getFileById(key);
var response = UrlFetchApp.fetch("https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=" + key +
"&gid=" + gid + "&exportFormat=ods", oAuth());
var fileBlob = response.getBlob();
return fileBlob;
}
function oAuth() {
var oauthConfig = UrlFetchApp.addOAuthService("spreadsheets");
var scope = "https://spreadsheets.google.com/feeds"
oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oauthConfig.setAuthorizationUrl("https://accounts.google.com/OAuthAuthorizeToken");
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
var requestData = {
"oAuthServiceName": "spreadsheets",
"oAuthUseToken": "always",
};
return requestData;
}

What are the exact steps to read a csv file into an multidimensional array in Node.js?

I have the following data in a .csv file:
Smith,Tom,43
Johnson,Sue,28
Sommers,Jeff,44
I would like to populate a multi-dimensional array with this data so that:
array[0][0] == Smith
array[1][2] == 28
etc.
Using the csv module -
var csv = require('csv');
var people = [];
var filePath = 'path/to/my/awesome/csv/file.csv';
csv()
.from.path(filePath)
.to.array(function(data) {
data.forEach(function(person, index) {
people[index] = [];
people[index][0] = person[0];
people[index][1] = person[1];
}
}
I also tried the following (using ya-csv ) and like it as well:
function readFile() {
var $IN = require('ya-csv');
var $filePath = 'data.csv';
var $people = [];
var $reader = $IN.createCsvFileReader($filePath, {
'separator': ','
});
$reader.on('data', function(item) {
$people.push(item);
})
}

Resources