Smart Sheet API 2.1.1, Download attachment but url is missing - smartsheet-api

Download attachement to Local drive ##
for(Row r : rows){
PagedResult<Attachment> attachments = smartsheet.sheetResources().rowResources().attachmentResources().getAttachments(sheetId, r.getId(), null);
System.out.println("attachments"+attachments.getData().size());
if(attachments.getData().size() > 0){
for(Attachment at : attachments.getData()){
System.out.println(String.format("Attachement ID[%s], URL{%s}",at.getId(),at.getUrl()));
}
Output::
Attachement ID[2625191160375172], URL{null}
Attachement
ID[3215061968807812], URL{null}
Attachement ID[6310196327802756],
URL{null}
Attachement ID[5565993922652036], URL{null}

I understand now, iterating the object and reaching out the attachement will not give you the url, infact you have to get id from that object and then query smart sheet again with GetAttachment procedure i. e
Attachment at = smartsheet.sheetResources().attachmentResources().getAttachment(sheetId, att.getId());

Related

Link content type to funtion on Telebot Python

I am building a Telegram Bot using telebot with python. I ask users to updload a picture, for which I created a command "foto". When they do /foto, I ask them to updload a content type "photo". However, they can upload pictures regardless of the command; they just click on upload a picture and that's it. How can I link the "upoload photo" to the command, so that they can only upload a picture after writing "/foto"?
#bot.message_handler(commands=['foto']) def send_welcome(message,
chat_id):
chat_id = message.chat.id
send = bot.send_message(message.from_user.id, 'Subí tu foto.')
bot.register_next_step_handler(send, photo())
return
#bot.message_handler(content_types=["photo"]) def photo(message):
fileID = message.photo[-1].file_id
logger.info("User_id, fileID", message.from_user.id, fileID)
file_info = bot.get_file(fileID)
#print('file.file_path =', file_info.file_path)
downloaded_file = bot.download_file(file_info.file_path)
with open(f"./images/{fileID}.jpg", 'wb') as new_file:
new_file.write(downloaded_file)
send = bot.send_message(message.from_user.id, 'Foto recibida, gracias!')
As far as i know you cant restrict users from sending files
what you can do is to capture the files only after the command is used
for more info please read docs

Email Pdf attachments to Google sheet

I am looking for something that allows me from a mail PDF attachment to get a data in a google sheet.
We all often get PDF attachments in our email and it will be great if we get the entire data in a google sheet.
DO let me know if there is anything like this
Explanation:
Your question is very broad and it is impossible to give a specific answer because that answer would depend on the pdf but also on the data you want to fetch from that, besides all the other details you skipped to mention.
Here I will provide a general code snippet which you can use to get the pdf from the gmail attachments and then convert it to text (string). For this text you can use some regular expressions (which have to be very specific on your use case) to get the desired information and then put it in your sheet.
Code snippet:
The main code will this one. You should only modify this code:
function myFunction() {
const queryString = "label:unread from example#gmail.com" // use your email query
const threads = GmailApp.search(queryString);
const threadsMessages = GmailApp.getMessagesForThreads(threads);
const ss = SpreadsheetApp.getActive();
for (thr = 0, thrs = threads.length; thr < thrs; ++thr) {
let messages = threads[thr].getMessages();
for (msg = 0, msgs = messages.length; msg < msgs; ++msg) {
let attachments = messages[msg].getAttachments();
for (att = 0, atts = attachments.length; att < atts; ++att) {
let attachment_Name = attachments[att].getName();
let filetext = pdfToText( attachments[att], {keepTextfile: false} );
Logger.log(filetext)
// do something with filetext
// build some regular expression that fetches the desired data from filetext
// put this data to the sheet
}}}
}
and pdfToText is a function implemented by Mogsdad which you can find here. Just copy paste the code snippet provided in that link together with myFunction I posted in this answer. Also you have some options which you can use that are very well explained in the link I provided. Important thing to note, to use this library you need to enable the Drive API from the resources.
This will get you started and if you face any issues down the road which you can't find the solution for, you should create a new post here with the specific details of the problem.

Generate and send pdf through Google Forms to my email address - doesn't send, debugging to no assitance

I'm trying to send myself a form-based report as a pdf. The problem is, I don't receive any emails. Debugging doesn't help much, since that only tells me which values are "undefined" (they are being defined the instant one fills out the form and triggers the email by clicking send; in theory). My coding experience stems from the days of TurboPascal and .bat-files, and I have lately realised I need to shape up. Trying to figure out Android, and this is a little experiment at work. But I had forgotten the lost feeling of "what now?"...
Here's the code:
// Samfunnsutvikling kursrapport
var docTemplate = "TemplateIDinGoogleDoks";
var docName = "Kursrapport";
// When Form Gets submitted
function onFormSubmit(e) {
//Get information from form and set as variables
var email = "worker#work.no";
var namn = e.namedvalues.namn;
var arrangement = e.namedvalues.arrangement;
var dato = e.namedvalues.dato;
var referat = e.namedvalues.referat;
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DocsList.getFileById(docTemplate)
.makeCopy(docName+' for '+namn)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();
// Replace place holder keys,in our google doc template
copyBody.replaceText('keynamn', namn);
copyBody.replaceText('keyarrangement', arrangement);
copyBody.replaceText('keydato', dato);
copyBody.replaceText('keyreferat', referat);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF
var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = "Kursrapport";
var body = "Kursrapporten frå " + namn + "";
GmailApp.sendEmail(email, subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DocsList.getFileById(copyId).setTrashed(true);
}
The script/document is authorized to send emails, but, oddly, I had to authorize it twice. It is saved.
In general, you can add Logger.log() to print variable values or just messages to see how far does it reach.
It looks as attachments parameter expects Blob[] type, however you are passing just Blob. So, it should be:
GmailApp.sendEmail(email, subject, body, {htmlBody: body, attachments: [pdf]});
You may also want to replace DocsList with DriveApp class as-is since the first one is depricated.
Update
It turned out also that getActiveSection() was renamed to getBody().

Docx to Google Doc using Google Script

I have a lot of docx files ( ~ 72 ) , their MIMETYPE is :
application/vnd.openxmlformats-officedocument.wordprocessingml.document
and I want to convert all of them to GoogleDocument so I can edit them online to do so I need them to be
application/vnd.google-apps.document
Is this possible ? I've tried with blob but nothing works , there is a way to create a new google doc with the same format ?
Got it .
var docx = DriveApp.getFileById("###");
var newDoc = Drive.newFile();
var blob =docx.getBlob();
var file=Drive.Files.insert(newDoc,blob,{convert:true});
The {convert:true} convert it .
Ps : you need to set document name since it's not included in teh blob
DocumentApp.openById(file.id).setName(docx.getName());

How to mail a screen captured image using corona SDK

I am new to corona SDK, but I've managed to capture my app scene using the following code:
local function captureArea()
local myCaptureImage = display.captureBounds(display.currentStage.contentBounds, true)
myCaptureImage:removeSelf()
myCaptureImage = nil
end
bg:addEventListener("tap",captureArea)
This works perfectly.
Now I need to send the captured image(with a specific name, say: screen_1.png) to my friend via email. I've used Composing E-mail and SMS for refference, but I fail to understand how I can add this saved image in the attachment field of mail options.
Please give me a proper solution that how can I attach and send the above saved image via email.
display.captureBounds is good for saving the whole screen to the directory. But it usually saves the file with increase in last index. So it may be difficult to read them correctly. So I prefer display.save. But it is not a straight way.
For doing this, you have to:
First create a localgroup.
Then add the screen objects to that group.
Return the display group
Use display.save to save the entire group displayed.
Create mail option and add attachment image from baseDirectory
Call mail Popup
I am giving a sample here:
-- creating the display group --
local localGroup = display.newGroup()
-- creating display objects and adding it to the group --
local bg = display.newRect(0,0,_w,_h)
bg.x = 160
bg.y = 240
bg:setFillColor(150)
localGroup:insert(bg)
local rect = display.newRect(0,0,50,50)
rect.x = 30+math.random(260)
rect.y = 30+math.random(420)
localGroup:insert(rect)
-- Then do as follows --
local function takePhoto_andSendMail()
-- take screen shot to baseDirectory --
local baseDir = system.DocumentsDirectory
display.save( localGroup, "myScreenshot.jpg", baseDir )
-- Create mail options --
local options =
{
to = { "krishnarajsalim#gmail.com",},
subject = "My Level",
body = "Add this...",
attachment =
{
{ baseDir=system.DocumentsDirectory, filename="myScreenshot.jpg", type="image" },
},
}
-- Send mail --
native.showPopup("mail", options)
end
rect:addEventListener("tap",takePhoto_andSendMail)
This will do it...
Keep coding........ :)