I have this code that search new doc in drive folder, and send files via email
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var email = "xxx#gmail.com";
var timezone = ss.getSpreadsheetTimeZone();
var today = new Date();
var oneDayAgo = new Date(today.getTime() - 1 * 24 * 60 * 60 * 1000);
var startTime = oneDayAgo.toISOString();
var search = '(trashed = false or trashed = false) and (modifiedDate > "' + startTime + '")';
var folder1 = DriveApp.getFoldersByName('SaveToPDF').next();
var files1 = folder1.searchFiles(search);
var row = "", count=0;
while( files1.hasNext() ) {
var file1 = files1.next();
var fileName = file1.getName();
var fileURL = file1.getUrl();
var lastUpdated = Utilities.formatDate(file1.getLastUpdated(), timezone, "yyyy-MM-dd HH:mm");
var dateCreated = Utilities.formatDate(file1.getDateCreated(), timezone, "yyyy-MM-dd HH:mm")
row += "<li>" + lastUpdated + " <a href='" + fileURL + "'>" + fileName + "</a></li>";
sheet.appendRow([dateCreated, lastUpdated, fileName, fileURL]);
count++;
}
if (row !== "") {
row = "<p>" + count + " file(s) have changed in your Google Drive in the past 24 hours. Here's the list:</p><ol>" + row + "</ol>";
row += "<br><small>To stop these notifications, please <a href='" + ss.getUrl() + "'>click here</a> and choose <em>Uninstall</em> from the Drive Activity menu.<br/></small>";
MailApp.sendEmail(email, "Google Drive - File Activity Report", "", {htmlBody: row, cc: "xxx#gmail.com"} );
}
I need to convert sheet files to XLSX format before send.
Can someone help me?
Thanks
You can follow this tutorial on how to convert the current Google Spreadsheet to Excel XLSX format and then emails the file as an attachment to the specified user using getGoogleSpreadsheetAsExcel() method.
function getGoogleSpreadsheetAsExcel(){
try {
var ss = SpreadsheetApp.getActive();
var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + ss.getId() + "&exportFormat=xlsx";
var params = {
method : "get",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions: true
};
var blob = UrlFetchApp.fetch(url, params).getBlob();
blob.setName(ss.getName() + ".xlsx");
MailApp.sendEmail("amit#labnol.org", "Google Sheet to Excel", "The XLSX file is attached", {attachments: [blob]});
} catch (f) {
Logger.log(f.toString());
}
}
Here are some similar threads which might help:
Google apps script to email google spreadsheet excel version
Converting .xls to google spreadsheet in google apps script
Related
I trying to convert pdf file to pdfa3 file by using PDFTron.
I added current url_path.
the my code below:
var input_url = './utils/';
var input_filename = 'test.pdf';
var output_filename = 'test_pdfa.pdf';
var convert = true;
var pwd = '';
var exceptions;
var max_ref_objs = 10;
var url_input = input_url + input_filename;
console.log('Converting input document: ' + input_filename);
var pdfa = await PDFNet.PDFACompliance.createFromUrl(true, url_input, '', PDFNet.PDFACompliance.Conformance.e_Level2B, exceptions, max_ref_objs);
get error:
'NetworkError(Unsupported protocol ${this._url.protocol})',
Does anyone know what the problem is,
And why doesn't it recognize the location?
I changed the code to :
here.
Now it's working!!
I have writtten code like below to log express logs.
const rfs = require("rotating-file-stream");
function formatDate() {
var d = new Date(),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
hour = d.getHours();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
if (hour.length < 2)
hour = '0' + hour
return [year, month, day, hour].join('-');
}
let log_directory = '/../logs/';
let log_date = formatDate()
let log_file_name = path.normalize(__dirname + log_directory + log_date, 'access.log');
let accessLogStream = rfs.createStream(log_file_name, {
size: "300M",
interval: "1d",
})
app.use(morgan(':date[iso] :method :url :body :headers :remote-addr :req[content-length] - :status :response-time ms - :res[content-length] ', { stream: accessLogStream }));
logs are getting stored out side of the project as expected.
But after rotation, the logs are storing inside the project with the below folder names
20200507-0943-01-
20200508-0943-01-
20200509-0943-01-
20200510-0943-01-
20200511-0943-01-
20200512-0943-01-
20200513-0944-01-
package.json
package-lock.json
public
routes
scripts
services
app.js
I don't want these folders created inside the project. Is there any way to handle this?
First of all you are using a static file name as filename parameter of createStream function and you are doing some computation on date to have your resulting file name.
Please note that the computation on date happens only once, before calling the createStream function. In order to do computation on date happening at each rotation you should use a filename generator function. Details can be found in readme.
More than this it seems there is a bug in this line: path.normalize accepts only one parameter.
let log_file_name = path.normalize(__dirname + log_directory + log_date, 'access.log');
Last, to specify a directory for logging, path option should be enough.
If I argued correctly your intentions, following code should solve all the problems
const rfs = require("rotating-file-stream");
function formatDate(d) {
var month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear(),
hour = d.getHours();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
if (hour.length < 2)
hour = '0' + hour;
return [year, month, day, hour].join('-');
}
function log_file_name(time, index) {
if (!time) return 'access.log';
return [formatDate(time), index, 'access.log'].join('-');
}
let accessLogStream = rfs.createStream(log_file_name, {
size: "300M",
interval: "1d",
path: '/../logs/'
});
I'm trying to convert a xls file to a google spreadsheet with the following code:
var searchFor ="fullText contains 'theFile.xls' and '" + FolderId + "' in parents";
var files = DriveApp.searchFiles(searchFor);
var file = files.next().getBlob();
var convertedFileinfo = {
title: 'theFile',
parents:[{id: FolderId}]
};
Drive.Files.insert(convertedFileinfo, file, {
convert: true,
uploadType:'resumable'
})
But i'm getting "Empty response" error on Drive.Files.insert line...
What I'm doing wrong?
How about following change?
From :
Drive.Files.insert(convertedFileinfo, file, {
convert: true,
uploadType:'resumable'
})
To :
Drive.Files.insert(convertedFileinfo, file, {
convert: true
})
I finally get it to work using copy:
var searchFor ="fullText contains 'theFile.xls' and '" + FolderId + "' in parents";
var files = DriveApp.searchFiles(searchFor);
var xlsFile = files.next();
Drive.Files.copy({}, xlsFile.getId(), {convert: true});
I am taking snapshot in html5 using the following code.
window.takeSnapshot = function(){
$("body").append("<canvas id='dummyCanvas' class='canvas' style='display: none'></canvas>");
var canvas = document.getElementById("dummyCanvas");
canvas.width = videoWidth;
canvas.height = videoHeight;
var context = canvas.getContext("2d");
var type = mediaType;
var tp = tupple;
context.drawImage(videoElement, 0, 0, videoWidth, videoHeight);
var contents = canvas.toDataURL('image/png');
var dt = new Date();
Message.showProgress();
var time = dt.getHours() + "_" + dt.getMinutes() + "_" + dt.getSeconds();
var file = {name: "Snapshot_" + time + ".png", contents: contents, recorded: true};
var id = "attachment_" + window.Guid();
var icon = (type==ContentTypes.Video)?("video.png"):((type==ContentTypes.Audio)?"audio.png":"image.png");
$("#attachments").append("<tr id='"+id+"'><td align='right'><img src='assets/images/progress.gif' style='width:16px'/></td><td><img src='assets/images/" + icon + "' style='width: 14px;' /></td><td style='font-size: 8pt; font-family: Arial; font-weight: bold;' style='text-decoration:none; color: #000000'>"+file.name+"</td></tr>");
Logger.log("Uploading " + file.name + " ...", LogTypes.INFO);
$("#mediaPanel").remove();
$("#attachmentPopup").show();
window.stream.stop();
var callback = function(){
Logger.log("Upload completed for " + file.name + " !", LogTypes.INFO);
CWDocumentWriter.addAttachment(tp, file.name, type);
$("#"+id).find('td').eq(0).html("<a href='javascript:void(0)' title='Delete attachment' onclick='window.deleteAttachment(["+tp[0]+","+tp[1]+","+tp[2]+","+tp[3]+"],\""+file.name+"\", event)'><img src='assets/images/delete.png' style='width:16px'/></a>");
$("#"+id).find('td').eq(2).html("<a href='javascript:void(0)' title='"+file.name+"' onclick='window.showContent(\"" + file.name + "\", " + type + ")'>"+file.name+"</a>");
Message.hideProgress();
};
if(Cloud.isLive())
Cloud.writeFile(file, contents, callback);
else{
var canvasImage = canvas.toDataURL("image/png").split(',')[1];
var decodedImg = window.atob(canvasImage);
var img = new Buffer(decodedImg, encoding='base64');
file.contents = img;
StorageManager.writeImageFile(file, contents, callback);
}
};
The image is uploaded to the cloud or saved on the local storage (using nodejs functions) depending upon the live or dead internet. It works fine in uploading the image data to the cloud and I can save and see the image back. But in the case of local hard drive it is not working. The image seems to be corrupted. Here is how I am saving it.
StorageManager.writeImageFile = function(file, data, callback){
if(!UserManager.isLoggedIn()){
UserManager.login();
return;
}
var key = ProjectManager.projectName;
var dir = Settings.USER_FOLDER + "/" + key + "/" + "media";
data = window.encode64(file.contents);
fs.writeFile(dir + "/" + file.name, data, "base64", function(err){});
callback();
};
I have tried several approaches but it seems to be not working correctly. The image file is written in the hard drive but it is corrupted. Please help me to resolve this problem.
I believe this will solve your problem:
var imgData = canvas.toDataURL('image/png');
var data = imgData.replace(/^data:image\/\w+;base64,/, "");
var buf = new Buffer(data, 'base64');
var whereToSave = "C:\pathToSave"; // edit this
fs.writeFile(whereToSave, buf);
I am using a script file adapted from Amit Agarwal to send my gmail attachment to google drive.
I have a team of about 100 people that each sent a weekly report file in the format of MS excel as attachment to me. These emails are first labeled "Weekly" and the script file will process each by saving its attachment to google drive. Once done, the email will be labeled "Saved".
It works well for most the cases, but each week, I will have between 4 to 10 incidences when an outdated weekly report is saved instead of the latest.
This is so strange.
function sendToGoogleDrive() {
var gmailLabels = "Weekly";
var driveFolder = "Weekly Report";
var archiveLabel = "Saved";
var moveToLabel = GmailApp.getUserLabelByName(archiveLabel);
if ( ! moveToLabel ) {
moveToLabel = GmailApp.createLabel(archiveLabel);
}
var filter = "has:attachment -label:" + archiveLabel + " label:" + gmailLabels;
var threads = GmailApp.search(filter, 0, 5);
var folder = DriveApp.getFoldersByName(driveFolder);
if (folder.hasNext()) {
folder = folder.next();
} else {
folder = DriveApp.createFolder(driveFolder);
}
for (var x=0; x<threads.length; x++) {
var message = threads[x].getMessages()[0];
var desc = message.getSubject() + " #" + message.getId();
var att = message.getAttachments();
for (var z=0; z<att.length; z++) {
try {
var file = folder.createFile(att[z]);
file.setDescription(desc);
var oldFilename = file.getName();
var newFilename = (Utilities.formatDate(message.getDate(), "GMT+8", "yyyy-MM-dd'T'HH:mm:ss'Z'") + " " + oldFilename);
file.makeCopy(newFilename);
}
catch (e) {
Logger.log(e.toString());
}
}
threads[x].addLabel(moveToLabel);
}
}
I have just noticed that it might have to do with the "References:" field in the header of the message. The message that I have problem with has in its header the following lines:
.......
Subject: =?GB2312?B?d2Vla2x5IHJlcG9ydC24tb31z7w=?=
References: <201409191817238757191#bekencorp.com>,
<2014092618170700047620#bekencorp.com>,
<201412051803001092350#bekencorp.com>,
<201412121802092504091#bekencorp.com>,
<2014123117571921868416#bekencorp.com>,
<2015010912141701588718#bekencorp.com>,
<201501161011295938041#bekencorp.com>
X-Priority: 3
X-GUID: F196F58C-CD93-4EC3-BAEB-0987910BB9DB
X-Has-Attach: yes
X-Mailer: Foxmail 7, 2, 6, 37[cn]
Mime-Version: 1.0
Message-ID: <2015012318020417198016#bekencorp.com>
.......
and instead of saving to gDrive the attachment of message <2015012318020417198016#bekencorp.com>
, it keeps saving the attachment of message <201409191817238757191#bekencorp.com>, the first message listed in the "References:"