replace the filename of transferred file from gmail to gdrive with filenameDATE - date

I have script that can transfer email from gmail to gdrive and my plan is replace the filename of the email attachment to assigned filename with current date ddmmyy (Ex. project012322.xlsx) or .csv when the file is transferred to gdrive.
May I know what I need to modify or add with my script?
const searchItem = "in:inbox subject:(My Project) has:attachment";
const threads = GmailApp.search(searchItem, 0, 100);
const ids = threads.flatMap((thread) => {
const messages = thread.getMessages();
return messages.map((message) => {
const id = message.getId();
if (!values.includes(id)) {
const attachments = message.getAttachments({ includeInlineImages: false, includeAttachments: true });
attachments.forEach((attachment) => {
Drive.Files.insert({ title: attachment.getName(), mimeType: attachment.getContentType(), parents: [{ id: folderId }] }, attachment.copyBlob());
});
}
return [id];
});
});

Your script contains the line
Drive.Files.insert({ title: attachment.getName(), mimeType: attachment.getContentType(), parents: [{ id: folderId }] }, attachment.copyBlob()); with title: attachment.getName()
This means that you are creating a file on your Drive with the title that is exactly the same like the original name of the attachment
If instead you would like to give the file an assigned name plus the current date - you need to define the assigned name and get the current timestamp
For this, you can replace the line above by the following three lines:
var myName = "project";
var currentDate = new Date();
Drive.Files.insert({ title: myName + " " + currentDate, mimeType: attachment.getContentType(), parents: [{ id: folderId }] }, attachment.copyBlob()); ` with `title: attachment.getName()
Use new Date() to retrieve the current timestamp
If you would like to fromat the timestamp retrieved by new Date() - use Utilities.formatDate(date, timeZone, format)

Related

Mongo saving ISODate

Im currently saving the next date into a collection
2022-01-02T00:00:00-05:00
But somehow when I check the date in the db looks like this (adding 16 seconds)
2022-01-02T05:00:16.000+00:00
When It should be saved like this
2022-01-02T05:00:00.000+00:00
Someone know why is this happening?
EDIT: I found out that after I convert excel to json the 16 seconds are being added, this is how I convert excel to json with the package "convert-excel-to-json
"
const convertExcel = async (path) => {
const result = excelToJson({
sourceFile: path,
sheets: [
{
name: "Sheet1",
header: {
rows: 1,
},
columnToKey: {
A: "DATE",
},
},
],
});
Collection.insertMany(result.Sheet1);
};
Im getting the date like this:
{
DATE: 2022-01-02T05:00:16.000Z,
}
Excel:

How to generate queries and mutation string export from schema.graphql

I am building a flutter application and using amplify_flutter 0.2.1 and amplify v5.1.0
, when I pull the project from the Amplify-admin UI it generates a graphQL schema schema.graphql which is useless at front-end because in order to fetch or modify the document, every time we need to write the graphQL query document like this:
String graphQLDocument =
'''mutation CreateTodo(\$name: String!, \$description: String) {
createTodo(input: {name: \$name, description: \$description}) {
id
name
description
}
}''';
var operation = Amplify.API.mutate(
request: GraphQLRequest<String>(document: graphQLDocument, variables: {
'name': 'my first todo',
'description': 'todo description',
}));
Amplify Flutter official CRUD Doc
I want to write it like this:
const input = {
name,
description
};
const output = {
id,name,description
};
var graphQLDoc = createToDO(input,output); // it should return the string object according to the input and output passed.
var operation = Amplify.API.mutate(
request: GraphQLRequest<String>(document: graphQLDoc, variables: {
'name': 'my first todo',
'description': 'todo description',
}));
OR it can be advanced like this:
const input = {
name:"hackrx",
description: "this works cool"
};
const output = {
id,name,description
};
var graphQLQueryRes = await createToDO(input,output); // it should return the whole fetched object according to the output passed.
you can do this way that I use in my code:
String myMutation(name, description) {
var graphQLDocument = '''mutation CreateTodo {
createTodo(input: {name: $name, description: $description}) {
id
name
description
}
}
''';
return graphQLDocument;
}
var operation = Amplify.API.mutate(
request:
GraphQLRequest<String>(document: myMutation('john', 'doe')));
for getting the response:
you can run amplify codegen models in the terminal that generate data models, based on your schema.graphql tables. and after that, you can do it this way: e.g you have a user table
User.fromJson(operation.response.data['createToDo]);
now you have an object of user class.

How to give configurable URL in tableau WDC

I am trying to build a tabeau WDC.
this is my code
(function () {
var myConnector = tableau.makeConnector();
myConnector.getSchema = function (schemaCallback) {
var cols = [{
id: "month",
dataType: tableau.dataTypeEnum.string
}, {
id: "value1",
alias: "value1",
dataType: tableau.dataTypeEnum.float
}, {
id: "value2",
alias: "value2",
dataType: tableau.dataTypeEnum.float
}];
var tableSchema = {
id: "testfeed",
alias: "test Feed",
columns: cols
};
schemaCallback([tableSchema]);
};
myConnector.getData = function (table, doneCallback) {
$.getJSON('http://test.com/view?name=test&filters=[{"type":"number","id_equals":["123"]}]', function (resp) {
var feat = resp.DATA,
tableData = [];
// Iterate over the JSON object
for (var i = 0, len = feat.length; i < len; i++) {
tableData.push({
"MONTH": feat[I].month,
"ChargeEntryLag_NUMERATOR": feat[i]. value1,
"ChargeEntryLag_DENOMINATOR": feat[i]. value2
});
}
table.appendRows(tableData);
doneCallback();
});
};
tableau.registerConnector(myConnector);
$(document).ready(function () {
$("#submitButton").click(function () {
tableau.connectionName = "testFeed";
tableau.submit();
});
});
})();
my URL contains some filters as shown in the above code, so if U want to get data for a particular filter I have to hardcode it in URL and the use it.
In other word my URL is static , Is there a way to make it dynamic.
suppose I want the value of 'id' to be 10in my filter, for that I have to go the the WDC code and change it. can it be made configurable.
use tableau.connectionData to pass data. There is an example in this tutorial:
https://tableau.github.io/webdataconnector/docs/wdc_multi_table_tutorial
Typically you'd create a form. When you connect with the WDC in tableau desktop, you put in the URL of your form. The form will store the form vars in tableau.connectData. Your getData can then take those and create a custom Data Source inside tableau desktop for you.
- Mike

What's up with mongoose date/timestamps?

I've a problem. I've been trying 2 different approaches to get correct date after an item is added to the collection.
1. new Date()
2. Date.now()
Both examples has the same issue. Only first added document has correct date. The next one has exactly the same date. Why the date is not changing?
Here's my collection:
const commentSchema = new Schema({
author: {
type: Schema.Types.ObjectId,
ref: 'user'
},
addDate: {
type: Date,
default: Date.now()
},
content: String,
answers: [commentReplies],
articleID: {
type: Schema.Types.ObjectId,
ref: 'article'
}
});
My insertion code:
app.post('/api/comments/addComment', async (req, res) => {
const author = req.body.author,
content = req.body.content,
articleID = req.body.articleID;
const comment = await new Comment({
author, content, articleID
}).save().catch(err => {
console.log(err);
})
res.send();
})
Because doing that way mongoose use the date at the time the schema is created: it execute the Date.now() function and use the value as the default for every document.
Probably the better way to do it is to use a save hook and set the date programmatically in the hook.

Meteor: How can I retrieve the inserted _id inside a method?

I have a meteor method to insert a document. The id for that document serves as a url extension. After the document is created, I also send an email via the same method. In the email, I want to include a link to the url extension. How can I do that?
//create a video
Meteor.methods({
createVideo: function(doc) {
Videos.insert({title: doc.title, userId: this.userId, date: new Date()});
var emailData = {
message: "Click the link below to go to your video.",
buttontext: "View My Video",
buttonlink: "http://sample.com/vids/" + ???????
},
body = EmailGenerator.generateHtml("actionEmailTemplate", emailData);
Meteor.call("sendMailgunEmail",
"account#sample.com",
"sample",
[Meteor.user().emails[0].address],
"Your video is live!",
body
);
}
});
From Meteor Docs:
collection.insert(doc, [callback])
Insert a document in the collection. Returns its unique _id.
So you can grab the _id from your insert by storing it in a local variable
var videoId = Videos.insert({title: doc.title, userId: this.userId, date: new Date()});
Check out the Meteor documentation for collection.insert:
Insert a document in the collection. Returns its unique _id.
Just assign the return value of the insert to a variable you can reference later:
//create a video
Meteor.methods({
createVideo: function(doc) {
var videoId = Videos.insert({title: doc.title, userId: this.userId, date: new Date()});
var emailData = {
message: "Click the link below to go to your video.",
buttontext: "View My Video",
buttonlink: "http://sample.com/vids/" + videoId
},
body = EmailGenerator.generateHtml("actionEmailTemplate", emailData);
Meteor.call("sendMailgunEmail",
"account#sample.com",
"sample",
[Meteor.user().emails[0].address],
"Your video is live!",
body
);
}
});