Force google spreadsheet to wait until cell calculation finish and then export to pdf - pdf

In my google spreadsheet file I have many custom function, that perform requests to my server and download some data. I need to automatically export it to PDF and then send on particular email.
I wrote like this:
function sendPDF() {
var pdf = DriveApp.getFileById(SpreadsheetApp.getActive().getId()).getAs('application/pdf').getBytes();
var attach = {fileName:'MyPDF.pdf',content:pdf, mimeType:'application/pdf'};
MailApp.sendEmail("example#email.com", "PDF test", "test", {attachments:[attach]});
}
And add trigger on time, that will execute this function every hour. The problem is, that with one spreadsheet I get PDF without downloaded data, and with another spreadsheet I get We're sorry, a server error occurred. Please wait a bit and try again. (line 44, file "Code") error.
How can I force google spreadsheet to wait until data will be downloaded and after download send me final PDF?

Sadly, without the needed reputation I cannot comment above.
Why not having this as a separate function like you have now and just call the function in your main function that fetches the url and downloads the data.
You can even write the downloaded data on a separate spreadsheet and insert an IF function that when cells are not empty execute the pdf script! After executing the script clear() and loop if necessary.

Related

Is there a way to copy an entire Spreadsheet with only values and no formulas/links (Google Sheets)?

The issue I am facing is trying to automate my weekly occurrence of coping an entire Spreadsheet to make a copy of it. Each week I need to hit "File --> Make a copy --> share with same people" and after doing so on the copy I need to hit "Allow Access" multiple times for each table that requires access, if I do not do this, there is no data displayed.
So I am wondering if there is a way to create a copy of a Spreadsheet where the copy contains entirely plain text and no formulas or links that way all the data can be read as soon as a copy is made.
This could be a separate question, but if anyone also knows how to automate hitting the "allow access" button for multiple tables in the copy that would also be helpful.
*To give an idea of the layout, essentially I have a main Spreadsheet (the one I make a copy of) that references data from other Spreadsheets (that are linked to google forms), and then I make a copy of the main Spreadsheet, and in making this copy is where I am required to hit the access button for each table.
Answer:
You can do this with Apps Script.
Code Example:
function duplicateSpreadsheet() {
const idOfSheetToCopy = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
var file = DriveApp.getFileById(ifOfSheetToCopy).next()
const newFile = file.makeCopy()
SpreadsheetApp.openById(newFile.getId()).getSheets().forEach(function(sheet) {
sheet.getDataRange().setValues(sheet.getDataRange().getDisplayValues())
})
}
Code Rundown:
Define Template Sheet ID
Make a copy of the sheet using DriveApp
Get the ID of the newly created sheet and open it with SpreadsheetApp
Loop through all sheets in the new copy and replace all cell values for the cell's display value
Instead of pasting with "Ctrl+V" use "Ctrl+Shift+V" for pasting without formatting. This will ignore things like links, font, and font size.
Not sure about the allow access button.

How to auto fit columns (width) using Excel Destination in SSIS?

I am working on an SSIS-2012 project, where I am receiving dataset after executing a stored procedure and saving the data in an excel file using an "Excel Destination" task in data flow. It works well, however I have a problem that for some fields, the width of columns are not large enough to show the value. Some numbers are shown like ##### for example, or part of a sentence instead of whole the end user sees etc.
Usually I handle this problem in Visual studio using a code like:
for (int i = 1; i <= RAWDATA_COLUMNS.Length; i++)
{
worksheet.Column(i).AutoFit();
}
However, here I could not find a way to handle it in SSIS. Writing a script to receive the data and save it into Excel file is not an option, using Excel Destination task in data flow is a must. Any help or advice would be appreciated.

Getting a Screenshot From A Bloomberg Terminal

I have a project and it requires grabbing multiple screenshots from a Bloomberg Terminal and I'd like to automate it.
This what I have so far:
Dim abc As Variant
Dim CUSIP As String
CUSIP = Range("A1")
ch = DDEInitiate("winblp", "bbk")
Call DDEExecute(abc, "<blp-1>" & CUSIP & " mtge<GO>")
Call DDEExecute(abc, "<blp-1> CFG<Go>")
Call DDEExecute (abc, "blp-1 <copy>")
Call DDETerminate(ch)
This correctly gets me to the graph I want (in this case the CFG, or Cash Flow Graph), but from this point, I am unable to figure out a screenshot method. In Bloomberg, there are a few commands, but they all require the use of a mouse. I've seen some do it before where they are able to get the requisite screenshots, but I can't figure out how.
So basically, I can use excel and VBA to get Bloomberg to pull up the correct screen, but now I just need to figure out a way to grab snipshot for the thing.
It's worth mentioning that the copy line just copies the data, not an image (which is what I was hoping for).
One option is to send the screenshot to your personal email address and download it from there:
Run GRAB in blp-1
Enter your personal email address
Download it manually from there or use an IMAP library to do so automatically
Another option is to use a 3rd party grabbing utility that has command line options. For example, Snagit can be started with an argument that creates a screenshot: http://informationworker.ru/snagit10.en/snagit_4_technical_reference.4.35.html

Export Google sheet to pdf without notes

Generally, can you disable Notes in a Google sheet temporarily?
More detail:
I can use GAS to export sheets to PDF, no issue there. But I've added a timestamp function which adds a 'Note' to a cell when it is edited using the OnEdit built in trigger. This is great because I can see when each cell was edited last without messing with revision history.
Unfortunately when I print to PDF, manually or via GAS, I get this ugly last page that includes each comment listed numerically. Anyway around this besides deleting the notes when I want to export? number of pages will be variable so I can't try to remove all but the first page each time.
There's probably a better way, but I would copy the Spreadsheet, delete all notes, export to PDF and delete the copied Spreadsheet.
You can make sure that you add all your "comments" in the same column, and then hide that column(s) before downloading as PDF.
But the best advice is to use the "Comment" feature of Google Sheets, which allows you to add Comments linked to a cell or range, which are not visible when you download as a PDF. You can insert a "comment" by going to the desired cell and from the menu "Insert" choose the item "Comment", or simply right click the cell and choose "Insert Comment".
So, yes, there is not only a way, but multiple ways of doing this. (Sad to see people respond negatively, vaguely or inaccurately without proper knowledge or even trying to solve the problem, understanding the question or attempt an answer).
I Created a simple script to do it for me:
Go to tool->script editor to create it, then copy/paste the code.
After you add the Menu, you need to refresh to see it.
Use: Click on Custom Utilities->Copy No Notes then type in the name of the sheet, it will create "copy of sheet" that has no notes.
Warning: This script deletes a page named "Copy of [sheet]" where [sheet] is the name of the text you type in. (if it exists)
function onOpen(){
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Utilities').addItem('Copy No Notes', 'copyNoNotes').addToUi();
}
function copyNoNotes(){
var ui = SpreadsheetApp.getUi();
var response = ui.prompt('Copy Sheet', 'Source Sheet?', ui.ButtonSet.YES_NO);
if (response.getSelectedButton() == ui.Button.YES) {
var source = response.getResponseText();
var newSheet = SpreadsheetApp.getActive().getSheetByName("Copy of " + source);
if(newSheet != undefined){
SpreadsheetApp.getActive().deleteSheet(newSheet);
}
var sheet = SpreadsheetApp.getActive().getSheetByName(source);
newSheet = sheet.copyTo(SpreadsheetApp.getActive());
newSheet.clearNotes();
}
}

Google Apps Script on Form Submit Time Formatting Glitch/Fix

Background:
How: I suspect that this is a glitch within Google Form (submission process)/Spreadsheet, but may be part of the Date conversion utility of the Spreadsheet interface (and is an intended feature).
When entering a format in a text box in Google Forms, there is some sort of communication error between the Form submit and Response Spreadsheet, or pre-processing of the Form's data before it is sent to the spreadsheet. The glitch only seems to happen for data in a text field of the format ##:## TEXT where TEXT contains no '.' characters. For example: 4:15 pm will reproduce the glitch, but 4:15 p.m and 4:15 p.m. will not.
Result: An apostrophe character is added to the beginning of the string when it is put into the Spreadsheet (i.e. '4:15 pm) which throws off several sub-systems I have in place that use that time data. Here are two screenshots (sorry for the bad sizing on the second):
I'm 99% certain that the glitch is caused by the ##: combination.
Temporary Fix?: The real question is... how might I go about removing that pesky apostrophe before I start manipulating the time data? I know how to getValue() of a cell/Range. Assume I have the value of a cell in the following manner:
var value = myRange.getValue();
// value = '4:15 pm
How can I go about processing that value into 4:15 pm? A simple java function could be
value = value.substring(1); // Assuming "value" is a String
But in Google App Scripts for Spreadsheets, I don't know how I would do that.
Post-Script: It is necessary to post-process this data so that I don't have to lecture university faculty in the language department about inputting time format correctly in their forms.
Thanks in advance to those who can help!
How can I go about processing that value into 4:15 pm? A simple java
function could be
value = value.substring(1); // Assuming "value" is a String But in
Google App Scripts for Spreadsheets, I don't know how I would do that.
Google Apps Scripts uses Javascript which has the exact same method.
value = value.substring(1);
should return all except the first character.
More about Javascript substring at: http://www.w3schools.com/jsref/jsref_substring.asp
If you remove the ' in the spreadsheet cell the spreadsheet interface will convert this entry to a date object.
This might (or not) be an issue for you so maybe you should handle this when you read back your data for another use...
It doesn't happen when text is different (for example with P.M) simply because in this case the ' is not necessary for the spreadsheet to keep it as a string since the spreadsheet can't convert it to a date object (time value).
Artificial intelligence has its bad sides ;-)
edit :
You cant do this in an onFormSubmit triggered function using the javascript substring() you mentioned. If you're not familiar with that, here is the way to go :
To run a script when a particular action is performed:
Open or a create a new Spreadsheet.
Click the Unsaved Spreadsheet dialog box and change the name.
Choose Tools > Script Editor and write the function you want to run.
Choose Resources > Current project's triggers. You see a panel with
the message No triggers set up. Click here to add one now.
Click the link.
Under Run, select the function you want executed by the trigger.
Under Events, select From Spreadsheet.
From the next drop-down list, select On open, On edit, or On form
submit.
Click Save.
see doc here and here