How do I pull sec.gov interactive EDGAR data into google sheets? - google-apps-script

I am trying to pull data from Sec.gov's EDGAR system for specific financial statements of specific filings for specific companies.
The first problem I am having in doing this is trying to figure out how to generally pull data from, for example, this link into a table in google sheets: https://www.sec.gov/cgi-bin/viewer?action=view&cik=320193&accession_number=0000320193-20-000096&xbrl_type=v#
The second problem I am having is trying to pull such data, but from specific financial statements (like the statements of cash flows) in the "financial statements" section, as the link above only brings me to the cover page of the filing, and each separate tab does not have separate links.
I am new at scripting within google sheets and am struggling to even find how to start at such a script that can do the above. This script I found on reddit (https://www.reddit.com/r/googlesheets/comments/bbousq/help_with_a_script_that_fetches_data_from_secgov/) is the closest script I have found online for what I am trying to do; however, I am unable to make it work and I am unsure how to do so:
function secData(company, tablenum) {if (!company)
company = 'TROV';
var url = "https://www.sec.gov/cgi-bin/browse-edgar?CIK=" + company + "&owner=exclude&action=getcompany";
var result = UrlFetchApp.fetch(url).getContentText();
var reg = /<a href="\/cgi-bin\/viewer.+?cik=(\d+).+accession_number=([\d|-]+)/.exec(result);
var cik = reg[1];
var accession = reg[2];
return 'https://www.sec.gov/Archives/edgar/data/' + cik + '/' + accession.replace(/-/g, '') + '/R'+tablenum+'.htm';
}
Which uses this formula in the sheet itself:
=IF(NOT(ISBLANK(G1)), importhtml(secData(G1,4),"table",1), "")
Where:
company = company name/ticker
tablenum = number of financial statement table (3 might = balance sheet)
Overall, I am wondering how to pull sec.gov interactive EDGAR data into google sheets and am hoping someone can help me do so?

You have an Excel document link on every page. You download that and convert it to Sheets, it's fairly easy. Here's a snippet to get you started.

Related

dynamic values in websites and docs

Let's say my company has a website and it includes various text. One of the texts it includes is the name of my Director of HR. His/her name could appear 20 or 30 times in various pages and documents on our website. And, should there be a change and I hire a new Director of HR, someone'd have to do manually change those 20 or 30 values.
There's got to be a better way to do that. One option is a scrub the website of personal data in all instances but one, "Contact the Director of HR," and then have the one value "the director of HR is..." and hyperlink all the instances of the Director of HR to the Staff page. But I'm interested in dynamic values.
To begin with, let's say I have this Google Doc and this Google Sheet. How would I get the value on the Google Doc to change when I change the value on the Google Sheet?
You can generate a Doc from a Sheet using apps script.
You'll go to Tools -> Script Editor in your Sheet.
Every time you run this script it will generate a new Doc with the value present in your sheet.
function myFunction() {
var doc = DocumentApp.create('HR Sheet');
var data = SpreadsheetApp.getActive().getDataRange().getValues()
var body = doc.getBody();
body.appendParagraph(data[1][1]);
}
If you run this script it will generate a new sheet and put the name of the HR Director on the sheet.
Helpful Link:
Google Docs documentation with scripting
You want to do the following:
Get a list of ROLES and NAMES from a Sheet.
For each ROLE/NAME pair, look for a specific value in a Google Doc, and replace that value with the corresponding NAME.
You can use replaceText(searchPattern, replacement) to achieve this. It could be something like this:
function findAndReplace() {
// Get "Sheet1", where data is:
var sheet = SpreadsheetApp.openById("your-spreadsheet-id").getSheetByName("Sheet1");
// Get the document body:
var doc = DocumentApp.openById("your-doc-id").getBody();
// Get the "ROLE/NAME" values:
var values = sheet.getRange(2, 1, sheet.getLastRow() - 1, 2).getValues();
// For each "ROLE/NAME" pair, replace text:
values.forEach(row => body.replaceText("{{" + row[0] + "}}", row[1]));
}
Note:
In this sample, the values to look for in the Doc are built using each corresponding role, with this pattern: {{your-role-name}}. [ is a special character for regex, so I think using {{ is more appropriate for this situation.
Reference:
Body.replaceText(searchPattern, replacement)
These answers were great re: Google Docs (which was the question I asked). But re: Website, I found this really useful plugin, TablePress, and the extension, Single Cell Content Shortcode. Populating my Director of HR in a Wordpress Page is as easy as using the shortcode [table-cell id=1 cell=C3 /].
I'm playing with autorefresh now, which'd repopulate the uploaded Sheet from the URL regularly, so I couldn't have to reupload the Google Sheet every time.
Looks like it'll work 100% for my purposes of populating Google Sheet values on Wordpress pages.

google apps script: Create a spreadsheet where newly created doc IDs appear

Be gently, very noob at this. So I've just replaced a guy who got a promotion and I am a primary teacher who has only really vaguely dabbled with this. I have successfully created a script that will do a lot of the things I need (so now looking to finish the job). My app script creates planner for teachers on a large cale so that they can have them labelled and the week goes in and changes the info.
Voila:
var fichier = DriveApp.getFileById('IDGOESHERE');
var newFichier=fichier.makeCopy("YOURGRADE LITERACY PLANNER TERM 1 WEEK 1");
var num=newFichier.getId();
var doc=DocumentApp.openById(num);
var body = doc.getBody();
doc.replaceText("abc123","TERM 1 WEEK 1");
It now works fairly well but I didn't know how to repeat it and then add one to the text, so I just copied it out heaps of times (one per each school week, not the end of the world). Any feedback here is welcome.
My real question though is this: I now need to go into each documents (there will be hundreds if not thousands) and link every document to our school website. My predecessor had the links in a sheet and just slipped them on to our website. Is there anyway of expanding the script so that it can publish the web address or even google doc ID of each newly created document into a spreadsheet (or text / doc).
This may not be possible, but if it were that would be amazing, would save me having to manually link all of the files week by week, one for each teacher over the year.
THANKS!
Yes, there is. After you create the doc there are 3 very useful methods you could call:
var name = doc.getName(); //returns the name in a string
var id = doc.getId(); //returns the id in a string
var url = doc.getUrl(); //returns a url to the document as a string
In the drive app, you will probably want to set the sharing permissions.
file.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);
You will then want to open a spreadsheet and append a new row for the data
sheet.appendRow([name, id, url]);

Google Apps Script to count number of emails received yesterday, that has certain label, then save # daily to spreadsheet

Basically what the title says, but I want to:
1) Automatically count the number of emails received to my gmail account, that has a certain label on it
2) Count once everyday, even if zero
3) And report daily to a Google Spreasheet
4) So I can make a monthly report like such:
Date / #
Date / #
Date / #
.
.
.
Total for October / #
Average per day / #
I'm sure this is piece of cake using Google Script for script gurus, but I have no clue. Please teach me!
Open a new Untitled spreadsheet and go to Tools -> open Script editor and paste the code given below.
function CountEmail()
{
var label = GmailApp.getUserLabelByName("LabelName");
var labelname = label.getName();
var mails = label.getThreads();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var date = new Date();
sheet.appendRow([labelname,date,mails.length]);
}
To run the script daily you will need to set a project trigger in the app script.For that follow the steps given below:
Resources -> Current Project's Trigger -> Add trigger -> Time driven -> Hour timer -> select the time when you want the script to run.
The total number of emails and the average emails/day can be calculated in the spreadsheet itself by just using the Sum() function.
Referencing the code Suyash Gandhi has posted (citing it here in case it gets removed so there is no confusion).
NOTE: not my code!!! Credit to Suyash Gandhi
function CountEmail()
{
var label = GmailApp.getUserLabelByName("LabelName");
var labelname = label.getName();
var mails = label.getThreads();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var date = new Date();
sheet.appendRow([labelname,date,mails.length]);
}
NOTE: not my code!!! Credit to Suyash Gandhi
See the picture below
What you see here are 2 threads, 1 email in the bottom one, 3 emails in the top one and 1 draft in the top one. That given code will return a 2 here. If you wish to count all 4 (or 5 if you want the draft) you will need to use Gmail API (see reference here).
I have a script that gets all emails (every message) for a specific time period and outputs the receive date, sender, recipient and title to a spreadsheet. This is the code that actually fetches the emails. The rest of the code is mostly creating files, generating the query string and reset the script if it runs too close to 6 minutes.
queriedMessages =
Gmail.Users.Messages.list(userInfo.mail,
{
'q': queryString,
'pageToken': execProperties.nextPageId
});
userInfo.mail is the email address you are fetching the emails from. This is simply written like this because the script can be run with any account
queryString is a string that is used to search for emails and is exactly the same as you use in the gmail search box. So you would have label:labelname
pageToken is a code of the page of the search (basically what is needed when you click the next page button in gmail). It is returned as part of this function so you would be able to access it from queriedMessages.nextPageToken. So if you get more than 1 page, then you will need it to access the rest of the messages.
Also, keep in mind that you are getting all the messages fitting the query, so if you do this daily, you may want to include a trigger. Also, keep in mind that functions firing from triggers ignore your timezone (known bug), but you can figure out how to create a query that works for only 1 day fairly easily. Personally I just grab +1 day on the beginning and the end and just filter those messages out.

Split data into different Google Sheets files

I have this one main file in Google Sheets. Now, I need to split this data such that its gets sent to " X " number of new files ( not a separate sheet). The data that is being sent is a subset of the data from the main file.
Is it possible to create these new workbooks and populate them with this subset data?
I tried creating new sheets, but the script is only working on creating it within the currently active sheet, and I cant find anything that is copying a range to a new sheet.
so i did figure out how to work around this thing.
now i didnt want to create a copy of the file. i wanted it to work somewhat like importrange.
var target2 = SpreadsheetApp.openById("Sheet url");
var target_sheet2 = target2.getSheetByName("Sheet1");
var source_rangefor2 = source_sheet.getRange("A7:C11");
var target_rangefor2 = target_sheet2.getRange("A1:C5");
var values = source_rangefor2.getValues();
target_rangefor2.setValues(values);
this copies the said range from the main sheet to the given range in the target sheet.
Hope this helps other people.

How to show in Google Form value from Google Spreadsheet

I have connected Google Spreadsheet with Google Form but I don't know, how can I include values from Spreadsheet to Form and display them (which will be in my scenario free left space for course registration).
explanation picture: https://drive.google.com/file/d/0B4HOotJEv18lLTNiY3ZmcGl3T3M/view?usp=sharing
Expected outcome: course has 30 free seats for students and I want to show in my Google Form, how many free seats lefts for course registration.
Many thanks for your help, also script example will be very welcome.
You can create the form in AppScript with Forms Service and set the questions and choices in the script. Within the script you can also reference a Spreadsheet, so you should be able to read the data from a spreadsheet and put it onto a form that way.
Haven't actually tried it, but it'd probably look something like:
var form = FormApp.create('New Form');
var item = form.addCheckboxItem();
var ss = SpreadsheetApp.openById("<ID>");
var val = ss.getRange(<RANGE>).getValue();
item.setTitle('Question ' + val + ' Question');
item.setChoices([
item.createChoice('ONE'),
item.createChoice('TWO'),
item.createChoice('THREE')
]);
You can also check out this Quickstart and Video.