Exporting a Google Sheet into an Excel using Google script - excel

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');
}

Related

Automatically unzipping files on google drive with google app script

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);
}
}
}

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 :( ?

gcloud nodejs copy table

I can't copy a table to a different dataset, but I can do it to the same one. Am I doing anything wrong?
var project_id = "project"
var dataset_id = "testDb";
var dataset_bk_id = "testDbBk"
var table_id = "test";
var dataset = bigquery.dataset(dataset_id);
var dataset_bk = bigquery.dataset(dataset_bk_id);
var table = dataset.table(table_id);
var table_bk = dataset_bk.table(table_id);
var metadata = {
createDisposition:"CREATE_IF_NEEDED",
destinationTable: { //required if allowLargeResults = true
projectId:project_id,
datasetId:dataset_bk_id,
tableId:table_id
},
writeDisposition:"WRITE_TRUNCATE"
};
table.copy(table_bk, metadata, function(err, job, apiResponse) {
if (err){
reject(err.message);
}else{
fulfill("Table " + table_id + " copied.");
}
});
Another thing that concerns me is the fact that I'm not getting any error in that bit of code.

image upload with "post" in nodejs with request module

How do we upload an image received form the mobile app to another server using the request module in nodejs?
I have tried using the multipart module to extract the file and send it in formData attribute with the request module (post method). This doesn't seem to work.
Please Use following code, it has been tested in express. you can modify it according to your requirement
var path = require('path');
var util = require('util');
if (req.files.profile_image !== undefined) {
var file = req.files.profile_image;
var tmp_path = file.path;
var fileName = file.name;
var milliseconds = new Date().getTime();
var file_ext = fileName.substr((Math.max(0, fileName.lastIndexOf(".")) || Infinity) + 1);
var newFileName = requestedUser + '_' + milliseconds + '.' + file_ext;
var pathToFile = require('path').dirname(require.main.filename);
var mainPath = path.dirname(pathToFile)
var target_path = path.join(mainPath, 'public/uploads/users', newFileName);
var readStream = fs.createReadStream(tmp_path)
var writeStream = fs.createWriteStream(target_path);
util.pump(readStream, writeStream, function(err) {
if (err) {
//handle error
} else {
//successfully uploaded
}
});
} else {
//file not recieved
}
Thanks

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;
}

Resources