<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Start Coding Now - google-apps-script</title><link href="https://startcodingnow.com/" rel="alternate"/><link href="https://startcodingnow.com/category/google-apps-script/feed" rel="self"/><id>https://startcodingnow.com/</id><updated>2023-08-25T00:00:00-07:00</updated><entry><title>Logging in Google Apps Script</title><link href="https://startcodingnow.com/logging-in-google-apps-script" rel="alternate"/><published>2023-08-25T00:00:00-07:00</published><updated>2023-08-25T00:00:00-07:00</updated><author><name>Lance Goyke</name></author><id>tag:startcodingnow.com,2023-08-25:/logging-in-google-apps-script</id><summary type="html">&lt;p class="first last"&gt;Simple logging in Google Apps Script can be done with &lt;tt class="docutils literal"&gt;console.log&lt;/tt&gt;&lt;/p&gt;
</summary><content type="html">&lt;p&gt;I have some &lt;a class="reference external" href="/gmail-auto-archive"&gt;Google Apps Script code&lt;/a&gt; that archives emails. I can see if the code ran without errors in the execution log, but I would like to also see how many emails were archived on a run to know if it changed the state of my inbox.&lt;/p&gt;
&lt;p&gt;We can &lt;a class="reference external" href="https://developers.google.com/apps-script/guides/logging"&gt;use the execution log to do some simple logging.&lt;/a&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;archiveInbox&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;threads&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;GmailApp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'label:inbox from:(jobalerts-noreply@linkedin.com) older_than:4d'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;threads&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="s1"&gt;'Moving thread to archive: "%s"'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="nx"&gt;threads&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;getFirstMessageSubject&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="nx"&gt;threads&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;moveToArchive&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the snippet above, the &lt;tt class="docutils literal"&gt;console.log&lt;/tt&gt; statement gets pushed directly to the execution log. This will leave a note whenever a thread gets archived with the subject of the thread.&lt;/p&gt;
&lt;p&gt;To view the logs, check the execution log from the Executions sidebar menu.&lt;/p&gt;
</content><category term="google-apps-script"/></entry><entry><title>Auto-Archive Emails in Gmail</title><link href="https://startcodingnow.com/gmail-auto-archive" rel="alternate"/><published>2023-07-28T00:00:00-07:00</published><updated>2023-07-28T00:00:00-07:00</updated><author><name>Lance Goyke</name></author><id>tag:startcodingnow.com,2023-07-28:/gmail-auto-archive</id><summary type="html">&lt;p class="first last"&gt;Set a timer to clean your inbox of old emails&lt;/p&gt;
</summary><content type="html">&lt;div class="section" id="goal"&gt;
&lt;h2&gt;Goal&lt;/h2&gt;
&lt;p&gt;I'm looking for a job in software engineering and have signed up for job alerts from LinkedIn.&lt;/p&gt;
&lt;p&gt;I still, however, have a personal training business to run, and these emails are muddying my inbox, making it easier to miss important emails from my clients.&lt;/p&gt;
&lt;p&gt;Given these job alerts are time-sensitive, I am okay with archiving emails that have been in my inbox for three days.&lt;/p&gt;
&lt;p&gt;A good alternative would be to check these emails right away, but hey, sometimes I get busy!&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="starting-point"&gt;
&lt;h2&gt;Starting Point&lt;/h2&gt;
&lt;p&gt;I'm starting with &lt;a class="reference external" href="https://www.labnol.org/code/19786-cleanup-gmail-inbox"&gt;the following snippet from Amit Agarwal.&lt;/a&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;cleanInbox&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;delayDays&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;maxDate&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="ow"&gt;new&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nx"&gt;maxDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setDate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;maxDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getDate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;delayDays&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;threads&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;GmailApp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getInboxThreads&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;threads&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;threads&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;getLastMessageDate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;archiveInbox&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;threads&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;GmailApp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'label:inbox is:read older_than:2d'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;threads&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;                &lt;/span&gt;&lt;span class="nx"&gt;threads&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;moveToArchive&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="google-apps-script-project-setup"&gt;
&lt;h2&gt;Google Apps Script Project Setup&lt;/h2&gt;
&lt;ol class="arabic simple"&gt;
&lt;li&gt;Head to &lt;a class="reference external" href="https://script.google.com"&gt;https://script.google.com&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Make sure you're logged in with the account associated with the Gmail inbox you want to clean&lt;/li&gt;
&lt;li&gt;Click on "&lt;a class="reference external" href="https://script.google.com/u/1/home/projects/create"&gt;+ New Project&lt;/a&gt;"&lt;/li&gt;
&lt;li&gt;Click on the "+" button next to Services and add "Gmail" to give the script permission to use Gmail, an &lt;a class="reference external" href="https://developers.google.com/apps-script/guides/services/advanced"&gt;advanced service&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Your script will be written inside the &lt;tt class="docutils literal"&gt;code.gs&lt;/tt&gt; file that automatically opens when creating a new project.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="search-and-archive-script"&gt;
&lt;h2&gt;Search and Archive Script&lt;/h2&gt;
&lt;p&gt;Open up &lt;tt class="docutils literal"&gt;code.gs&lt;/tt&gt; under Files and enter your script. Here's how I adjusted Amit's code to serve my purposes:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;archiveInbox&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;threads&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;GmailApp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'label:inbox from:(jobalerts-noreply@linkedin.com) older_than:3d'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;threads&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="nx"&gt;threads&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;moveToArchive&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Read &lt;a class="reference external" href="https://developers.google.com/apps-script/reference/gmail"&gt;the documentation&lt;/a&gt; to learn more about the &lt;tt class="docutils literal"&gt;GmailApp&lt;/tt&gt;. For example, you may want to skip archiving and &lt;a class="reference external" href="https://developers.google.com/apps-script/reference/gmail/gmail-thread#moveToTrash()"&gt;move the thread straight to the trash&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="a-note-on-feature-design"&gt;
&lt;h2&gt;A Note on Feature Design&lt;/h2&gt;
&lt;p&gt;When searching for the features I wanted, it was helpful to take a step back from the Gmail Service documentation and think about how I would accomplish this task using the standard Gmail graphical user interface.&lt;/p&gt;
&lt;p&gt;For my situation, I would use search to filter emails by sender and date. I figured out who the sender was and used the date filtering similar to Amit's example.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="test-your-script"&gt;
&lt;h2&gt;Test Your Script&lt;/h2&gt;
&lt;p&gt;Once you think your code is correct, click on the "Save" button and then the "Run" button.&lt;/p&gt;
&lt;p&gt;An Execution log should open at the bottom. If everything worked, you should see "Execution started" and "Execution completed".&lt;/p&gt;
&lt;p&gt;You can add &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;console.log("message")&lt;/span&gt;&lt;/tt&gt; statements to tell you what's happening when the code is run.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="enable-time-trigger"&gt;
&lt;h2&gt;Enable Time Trigger&lt;/h2&gt;
&lt;p&gt;As it stands, the script is actually done.&lt;/p&gt;
&lt;p&gt;The issue is that it's not saving any time to have to open up Google Apps Script every time you want to archive a few emails.&lt;/p&gt;
&lt;p&gt;To run this script automatically, click on "Triggers" with the time clock icon in the leftmost sidebar.&lt;/p&gt;
&lt;p&gt;Then click "+ Add Trigger" and choose your settings. Mine were like this:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Choose which function to run on: &lt;tt class="docutils literal"&gt;archiveInbox&lt;/tt&gt;&lt;/li&gt;
&lt;li&gt;Choose which deployment should run: &lt;tt class="docutils literal"&gt;Head&lt;/tt&gt;&lt;/li&gt;
&lt;li&gt;Select event source: &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Time-driven&lt;/span&gt;&lt;/tt&gt;&lt;/li&gt;
&lt;li&gt;Select type of time based trigger: &lt;tt class="docutils literal"&gt;Day timer&lt;/tt&gt;&lt;/li&gt;
&lt;li&gt;Select time of day: &lt;tt class="docutils literal"&gt;Midnight to 1am&lt;/tt&gt;&lt;/li&gt;
&lt;li&gt;Failure notification settings: &lt;tt class="docutils literal"&gt;Notify me daily&lt;/tt&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you changed the function name or you have multiple, they should show up in this dialog. You can customize the time to fit your needs.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="troubleshooting"&gt;
&lt;h2&gt;Troubleshooting&lt;/h2&gt;
&lt;p&gt;When first running, Apps Script will tell you "Authorization required". You must review and allow permissions using Google Single Sign-On.&lt;/p&gt;
&lt;p&gt;If you forgot to add the Gmail service, the execution log will tell you that "Execution started", but it will hit an error of "Script function not found".&lt;/p&gt;
&lt;p&gt;If you haven't placed your script within a function, you will not be able to run it.&lt;/p&gt;
&lt;p&gt;If the code appears to run correctly, but you do not notice the desired outcome in your Gmail inbox, double check that the project was created under the correct account.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="conclusion"&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I'm seeing the power of Google Apps Script as someone using many Google services. Perhaps we can start utilizing Google Sheets and Docs more?&lt;/p&gt;
&lt;/div&gt;
</content><category term="google-apps-script"/><category term="gmail"/></entry><entry><title>Create Your Own YouTube Analytics Dashboard SUPER FAST with Google Apps Script</title><link href="https://startcodingnow.com/youtube-analytics-dashboard-with-google-apps-script" rel="alternate"/><published>2021-04-20T00:00:00-07:00</published><updated>2021-04-20T00:00:00-07:00</updated><author><name>Lance Goyke</name></author><id>tag:startcodingnow.com,2021-04-20:/youtube-analytics-dashboard-with-google-apps-script</id><summary type="html">&lt;p&gt;In my day job as a personal trainer, I find myself doing a lot of
repetitive tasks. When writing a training program, there’s SO MUCH
clicking to format a Google Sheet well enough to make a professional
product.&lt;/p&gt;
&lt;p&gt;So I started playing around with macros and found it easy …&lt;/p&gt;</summary><content type="html">&lt;p&gt;In my day job as a personal trainer, I find myself doing a lot of
repetitive tasks. When writing a training program, there’s SO MUCH
clicking to format a Google Sheet well enough to make a professional
product.&lt;/p&gt;
&lt;p&gt;So I started playing around with macros and found it easy to augment a
macro with &lt;a class="reference external" href="https://developers.google.com/apps-script"&gt;Google Apps
Script&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;When recording a macro, the code is saved in the “Script Editor”.&lt;/p&gt;
&lt;div class="figure"&gt;
&lt;img alt="Tools &amp;gt; Script editor will open the script editor." src="./images/google-sheets-script-editor-menu.png"/&gt;
&lt;p class="caption"&gt;Tools &amp;gt; Script editor will open the script editor.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="figure"&gt;
&lt;img alt="The Google Apps Script Editor." src="./images/google-apps-script-editor.png"/&gt;
&lt;p class="caption"&gt;The Google Apps Script Editor.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;And I come to find out that this Script Editor can do a lot for us!&lt;/p&gt;
&lt;p&gt;In this post, we’re going to make your own personal YouTube dashboard.&lt;/p&gt;
&lt;div class="section" id="table-of-contents"&gt;
&lt;h2&gt;Table of Contents&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="#table-of-contents"&gt;Table of Contents&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="#create-a-new-google-sheet"&gt;Create a New Google Sheet&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="#find-a-youtube-video-id"&gt;Find a YouTube Video ID&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="#write-the-script"&gt;Write the Script&lt;/a&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="reference external" href="#line-by-line-code-analysis"&gt;Line By Line Code Analysis&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="#add-the-youtube-service-to-the-script"&gt;Add the YouTube Service to the
Script&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="#run-the-script"&gt;Run the Script&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="#next-ideas"&gt;Next Ideas&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="create-a-new-google-sheet"&gt;
&lt;h2&gt;Create a New Google Sheet&lt;/h2&gt;
&lt;p&gt;We’ll need a place to store your YouTube data. Create a new sheet and
give it three headings in the first columns:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Video ID&lt;/li&gt;
&lt;li&gt;Title&lt;/li&gt;
&lt;li&gt;Views&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="figure"&gt;
&lt;img alt="The first three cells have the headers “Video ID”, “Title”, and “Views”." src="./images/new-google-sheet-with-three-column-names.png"/&gt;
&lt;p class="caption"&gt;The first three cells have the headers “Video ID”, “Title”, and “Views”.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="find-a-youtube-video-id"&gt;
&lt;h2&gt;Find a YouTube Video ID&lt;/h2&gt;
&lt;p&gt;First, we’re going to need a video to analyze.&lt;/p&gt;
&lt;p&gt;I went and grabbed one of my top performers: &lt;a class="reference external" href="https://www.youtube.com/watch?v=2TPwtBl6_KQ"&gt;Elbow Pain During Push
Ups&lt;/a&gt;. Let’s examine the
link:&lt;/p&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;https://www.youtube.com/watch?v=2TPwtBl6_KQ&lt;/span&gt;&lt;/tt&gt;&lt;/p&gt;
&lt;p&gt;That value after the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;?v=&lt;/span&gt;&lt;/tt&gt; is the ID of the video.&lt;/p&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;https://www.youtube.com/watch?v=**2TPwtBl6_KQ**&lt;/span&gt;&lt;/tt&gt;&lt;/p&gt;
&lt;p&gt;So we’re going to copy that for our Google Sheet and paste it into cell
A2.&lt;/p&gt;
&lt;div class="figure"&gt;
&lt;img alt="The video ID has been pasted in cell A2." src="./images/google-sheet-with-video-id-pasted-in.png"/&gt;
&lt;p class="caption"&gt;The video ID has been pasted in cell A2.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="write-the-script"&gt;
&lt;h2&gt;Write the Script&lt;/h2&gt;
&lt;p&gt;The inspiration for this post came from another &lt;a class="reference external" href="https://developers.googleblog.com/2015/10/a-day-at-office-automating-youtube_12.html?linkId=17889868"&gt;post by Wesley Chun on
the Google Developers
blog.&lt;/a&gt;
It’s a few years old now, but the code mostly works. The user interface
has changed a bit, though, so I’m going to walk your through it.&lt;/p&gt;
&lt;p&gt;Open up the Script Editor from your Google Sheet: Tools &amp;gt; Script Editor.&lt;/p&gt;
&lt;p&gt;And type in the following code:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
function getVideoInfo() {
  let sheet = SpreadsheetApp.getActiveSheet();
  let vid = sheet.getRange("A2").getValue();
  let data = YouTube.Videos.list('snippet, statistics', {id: vid});
  let item = data.items[0];
  let info = [item.snippet.title, item.statistics.viewCount];
  sheet.getRange("B2:C2").setValues([info]);
}
&lt;/pre&gt;
&lt;p&gt;Wesley does a great job walking through the code in the post, but I’ll
do so here for completeness.&lt;/p&gt;
&lt;div class="section" id="line-by-line-code-analysis"&gt;
&lt;h3&gt;Line By Line Code Analysis&lt;/h3&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;function getVideoInfo() {&lt;/tt&gt;&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Here we’re declaring the function we’re going to run.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;let sheet = &lt;span class="pre"&gt;SpreadsheetApp.getActiveSheet();&lt;/span&gt;&lt;/tt&gt;&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;This tells the script what sheet we’re working on and is tied to our
current sheet&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;let vid = &lt;span class="pre"&gt;sheet.getRange("A2").getValue();&lt;/span&gt;&lt;/tt&gt;&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;This selects the video ID we just pasted in. If we need it, we can
use the &lt;tt class="docutils literal"&gt;vid&lt;/tt&gt; variable.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;let data = &lt;span class="pre"&gt;YouTube.Videos.list('snippet,&lt;/span&gt; statistics', {id: &lt;span class="pre"&gt;vid});&lt;/span&gt;&lt;/tt&gt;&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Here we’re using the &lt;a class="reference external" href="https://developers.google.com/youtube/v3/docs"&gt;YouTube Data
API&lt;/a&gt; to grab info
about our video.&lt;/li&gt;
&lt;li&gt;We use the
&lt;a class="reference external" href="https://developers.google.com/youtube/v3/docs/videos/list"&gt;YouTube.Videos.list&lt;/a&gt;
method.&lt;ul&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;'snippet, statistics'&lt;/tt&gt; tells the API what we want it to give
us.&lt;/li&gt;
&lt;li&gt;The &lt;tt class="docutils literal"&gt;snippet&lt;/tt&gt; property contains the &lt;tt class="docutils literal"&gt;channelId&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;title&lt;/tt&gt;,
&lt;tt class="docutils literal"&gt;description&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;tags&lt;/tt&gt;, and &lt;tt class="docutils literal"&gt;categoryId&lt;/tt&gt; properties.&lt;/li&gt;
&lt;li&gt;The &lt;tt class="docutils literal"&gt;statistics&lt;/tt&gt; property contains the &lt;tt class="docutils literal"&gt;viewCount&lt;/tt&gt;,
&lt;tt class="docutils literal"&gt;likeCount&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;dislikeCount&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;favoriteCount&lt;/tt&gt;, and
&lt;tt class="docutils literal"&gt;commentCount&lt;/tt&gt; properties.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;And we pass in the video ID from the &lt;tt class="docutils literal"&gt;vid&lt;/tt&gt; variable we just set.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;let item = data.items[0];&lt;/tt&gt;&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;We’ve only asked for one video, so our response from the YouTube Data
API has only given us one video in our array (at index 0).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;let info = [item.snippet.title, item.statistics.viewCount];&lt;/tt&gt;&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Grab the title and view count from the &lt;a class="reference external" href="https://developers.google.com/youtube/v3/docs/videos#resource"&gt;JSON response
data&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;sheet.getRange("B2:C2").setValues([info]);&lt;/span&gt;&lt;/tt&gt;&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Select our B2:C2 range.&lt;/li&gt;
&lt;li&gt;Add the two values from the &lt;tt class="docutils literal"&gt;info&lt;/tt&gt; variable we just set.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="add-the-youtube-service-to-the-script"&gt;
&lt;h2&gt;Add the YouTube Service to the Script&lt;/h2&gt;
&lt;p&gt;This is where things start to look a little different from Wesley’s
original article. Here’s how we do it in April 2021.&lt;/p&gt;
&lt;p&gt;Click + in Services.&lt;/p&gt;
&lt;div class="figure"&gt;
&lt;img alt="The plus button for adding a new service." src="./images/google-sheets-script-editor-add-service.png"/&gt;
&lt;p class="caption"&gt;The plus button for adding a new service.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Scroll down and select the YouTube Data API.&lt;/p&gt;
&lt;div class="figure"&gt;
&lt;img alt="The Add a Service dialog box. The YouTube Data API v3 contains a link to the documentation link and can be identified with “YouTube”." src="./images/google-sheets-script-editor-add-service-dialog.png"/&gt;
&lt;p class="caption"&gt;The Add a Service dialog box. The YouTube Data API v3 contains a link
to the documentation link and can be identified with “YouTube”.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Then click “Add” and you should see it in the left menu.&lt;/p&gt;
&lt;div class="figure"&gt;
&lt;img alt="The YouTube service is showing." src="./images/youtube-service-in-google-apps-script-sidebar.png"/&gt;
&lt;p class="caption"&gt;The YouTube service is showing.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="run-the-script"&gt;
&lt;h2&gt;Run the Script&lt;/h2&gt;
&lt;p&gt;Then we can “Run” the script.&lt;/p&gt;
&lt;div class="figure"&gt;
&lt;img alt="The run button is towards the top of the window." src="./images/google-sheets-script-editor-run-script-2000x685.png"/&gt;
&lt;p class="caption"&gt;The run button is towards the top of the window.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;A dialog box will open asking for you to authenticate your Google
Account.&lt;/p&gt;
&lt;p&gt;You may also get a warning that executing the code from this sheet is
not safe. Chrome likes to warn you by hiding the ability to go on and
showing a blue “Back to Safety” button. I was able to bypass this by
clicking “Advanced” and proceeding anyways.&lt;/p&gt;
&lt;p&gt;Once you’ve done that, you might have to click “Run” one more time.&lt;/p&gt;
&lt;p&gt;When it runs, the Execution Log should open at the bottom of the window.&lt;/p&gt;
&lt;div class="figure"&gt;
&lt;img alt="Execution log shows execution started and completed." src="./images/google-apps-script-app-execution-started-and-completed-2000x1073.png"/&gt;
&lt;p class="caption"&gt;Execution log shows execution started and completed.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;And if all went well, you’ll see what I’ve pasted above.&lt;/p&gt;
&lt;p&gt;Check the Google Sheet…&lt;/p&gt;
&lt;div class="figure"&gt;
&lt;img alt="The video title has been placed in cell B2. The video view count has been placed in C2." src="./images/google-sheets-video-title-in-cell-2000x1073.png"/&gt;
&lt;p class="caption"&gt;The video title has been placed in cell B2. The video view count has been placed in C2.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;And there’s our data!&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="next-ideas"&gt;
&lt;h2&gt;Next Ideas&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Track your top ten videos and find their views.&lt;/li&gt;
&lt;li&gt;Track your content marketing videos to see which perform best.&lt;/li&gt;
&lt;li&gt;Test different methods of engagement to see which videos get the most
comments, likes, and dislikes.&lt;/li&gt;
&lt;li&gt;Send these stats to your Gmail.&lt;/li&gt;
&lt;li&gt;Store a new file for every month with the highest performing videos
of the month.&lt;/li&gt;
&lt;li&gt;Grab data from a playlist you’ve created (perhaps a tutorial series?)&lt;/li&gt;
&lt;li&gt;Retrieve the IDs for your most popular videos and get all the data
you want about them.&lt;/li&gt;
&lt;li&gt;Create visual tables and graphs inside your Google Sheet to better
visualize your data.&lt;/li&gt;
&lt;li&gt;Share your results with a friend to hold yourself accountable.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Any other ideas? How are you using Google Apps Script? Share in a
comment below!&lt;/p&gt;
&lt;/div&gt;
</content><category term="google-apps-script"/><category term="apis"/><category term="authentication"/><category term="gmail"/><category term="google"/><category term="google-sheets"/><category term="graphs"/><category term="ideas"/><category term="tables"/><category term="youtube"/><category term="youtube-data-api"/></entry></feed>