How to Display an Entry in a Label – Tkinter Python 3

Python 3, Tkinter 8.6. GUI examples in Windows 10

Probably one of the most common things to do when using a Graphical User Interface (GUI) is to display something from an entry from the user.

Below is a simple example that allows the user to input text in a Tkinter Entry field and when they click “Enter” or use the <Return> or <Enter> button it will be displayed in a Tkinter Label.

The end result will look a little like this:

Display Entry in a Label in Tkinter with Python 3

Continue reading “How to Display an Entry in a Label – Tkinter Python 3”

How to Center the Main Window on the Screen in Tkinter with Python 3

Python 3, Tkinter 8.6. GUI examples in Windows 10

When your first window loads in Tkinter it will generally appear slightly offset from the top left hand corner of the screen. This is a fairly counter intuitive location and most of the GUI driven programs that I run usually open at the center of the page or a little higher than center.

If you want a primer of window positioning, check out the following tutorial:

How Do I Change the Size and Position of the Main Window in Tkinter and Python 3

In Python 3, to put the main window in the center of the screen I use the following code:

Window Centered on the Screen in Tkinter with Python 3
Output in Windows 10

Continue reading “How to Center the Main Window on the Screen in Tkinter with Python 3”

How Do I Change the Size and Position of the Main Window in Tkinter and Python 3

Python 3, Tkinter 8.6. GUI examples in Windows 10

When you create your first window in Tkinter, you can set it’s starting size and position on the screen by using the geometry method.

When using this method, note that it only provides the window with the size and position when it is initialized. This means that the user can then change the size or the window and move it once it has first been put on the screen.

Initial Window Size

Continue reading “How Do I Change the Size and Position of the Main Window in Tkinter and Python 3”

Add the Current Date to a Sheet When Data Is Added So That The Date Does Not Change(Static) – Google Sheets

You’ve probably come across the problem where you need to know when a piece of data has been added to your spreadsheet. You probably have been equally frustrated that there is no out-of-the-box function that will do just this.

You’ve tried TODAY() and NOW(), but they change dynamically. What you really need here is something that does not change.

Let’s look at two workarounds that can help you out with this problem.

Continue reading “Add the Current Date to a Sheet When Data Is Added So That The Date Does Not Change(Static) – Google Sheets”

How to Get Something from Google Sheets and Display it in the Sidebar in Google Apps Script

What if you want to get a value or a range from Google Sheets and show it in your sidebar using Google Apps Script?

First you will need to get the value or range by using Googles server-side script. Then you will have to display it client-side in your HTML document.

Documentation on getting the server-side and client-side talking nicely to each other is a little vague. Hopefully, this very basic tutorial will help clear things up.

In this tutorial I will also be using Jquery.

Let’s get started.

Continue reading “How to Get Something from Google Sheets and Display it in the Sidebar in Google Apps Script”

Add the Current Date When Data is Added to a Cell(Dynamic) – Google Sheets

A colleague of mine had recently asked me if there was a way to automatically display the day’s date when a cell has data in it without having to use Google Apps Script.

My answer: “Why yes, there is Jim.”

There are two really cool tools that you can use in Google Sheets to get the current date or time and date:

The NOW formula provides a date and time stamp at the time of execution or when the NOW() function is entered into the sheet.

NOW Google Sheets

The TODAY formula provides just the date at the time of execution or when the TODAY() formula is entered into the sheet.

TODAY Google Sheets

That’s all well and good but I want this formula to occur when a user enters something in a cell.

Done:

To achieve this, we can use an IF statement. First, we will state if the cell is empty, then no date should appear. However, if the cell has a value in it, then the date and/or time the value was entered will appear in the corresponding cell.

Here’s the formula:

=IF(A2 ="", "", TODAY())  <<Formula is celll D2

ِAs you can see in the  IF statement we are saying; if there is nothing in cell A2 then don’t do anything, otherwise if it contains a value then add today’s date in this cell, D2.

Now, if you want to have this ready for all rows for when a user adds to your data, then drag the formula all the way down to the bottom of the sheet.

Here is an example:

IF value DATE Google Sheets

Ready to add some data? Here is what is would look like:

Current Date Added When Cell is Entered

The NOW() and TODAY() functions are Dynamic. This means that they will update automatically. To keep these dates from changing, copy the cell and press <CTRL>+<SHIFT>+<V> or paste special> paste as value.

A Warning on NOW() and TODAY()

These two functions are Dynamic and will update constantly. If you are looking to get the date or time of something so that it does not update automatically then check out this tutorial :

Add the Current Date to a Sheet When Data Is Added So That The Date Does Not Change(Static) – Google Sheets

 

 

 

You can do exactly the same thing with the NOW formula. Give it a try.

Google Apps Script – Iterating Through Ranges in Sheets the Right and Wrong Way

I was trying to rush out some Google Apps Script code to deal with a task on Google Sheets recently. Basically, I had to search through a heap of data and find certain values and do something too them.

My column was reaching across the page to something like Column BK and my rows were over 1000 deep. Running this code was taking forever!!!

My immediate instinct was:

What have I done wrong?

…and my instinct was right.

The Good and Bad Way to Search Through Code

So after looking at my code again, I discovered that for some reason I go it into my head that I should be searching each cell for the value I needed and then doing something with it.

Sounds logical right? It’s sorta what you are meant to do.

The problem is that I was calling the sever and asking for the range in each cell as I was looping through the entire document. This is super costly and inefficient in terms of time.

Google talks about this in their Google Apps Script Best Practice page under Batch Operations.

Also, if you do run a costly code like this, then you will get a little red light in your Script tool bar that represents your Execution Hints:

Google Apps Script Execution Hints

Clicking on Execution Hints and expanding the side bar with provide you with a far-too-deserving-polite dressing down about your slow and server costly code.

Method Range.getValue is heavily used

The Good

So instead of calling the server for each cell I need to get the full range of the data I am working on, search through it client-side to find what cells require modifying and then invoke the modifications.

Let’s look at a simple example:

Example

In this example I want to search through all the results over multiple quizzes and if there is a dash “-” or a zero “0” change the background accordingly.

Yeah! Yeah! I can do this with conditional formatting, but this is an example, damn it, Jim!!!

Below is a sample of the data I will use and here is the link.

Both Good and Bad examples have the same end result. The result should look like below:

First let’s set up the Google Apps Script code file by calling the user interface, sheet and range of data. (Lost? Create your first Apps Script)

Hopefully everything is self-explanatory here. We call the spreadsheet first and then look for the active sheet. Inside the active sheet we want the range of the data (rangeData) which will contain all the data in the range. We will use that data to get the last row and column number of the data. Finally we will call the server to get he range we want to work with (searchRange).

Once done, we will create our function, onOpen(). When it is called it will create a menu called Checker with the sub menu Bad Way and Good Way. This isn’t necessary, but it might be easier for you to physically test the difference in the slow method versus the fast (correct) one.

The Bad Way

As mentioned above in the Bad (slow) Way we call the server each time to look at what is in a cell.

As I loop across the columns and then the rows, I am using my search range to get the value of the cell in Line 25. This means I am contacting the server a total for 436 times. This significantly slows things down.

The Good Way

 

In the preferred approach I am taking the array that I created from searchRange.getValues() in Line 36  and searching through it before I make my calls to change the background when a dash or a zero occurs.

Why is this better?

I only make server call to collect the range data once. Then client-side (on my computer in this instance), I do all my searching before calling Google who collects all the changes in a cache until the loops are done before creating background colors all at once.

Super fast.

Speed comparison. 

Take a look at the speed differences over ten tests:

The Good Way is the clear winner. You can try it out for yourself if you have been playing along by going to <View><Execution Transcript> in the Script

Take Home

The take home from this is that, make as little calls to the server as possible. It significantly improves your speed.

The Full Code

 

 

 

 

 

 

Skewed Random Range in Google Sheets (RANDBETWEEN)

The Standard RANDBETWEEN

We can achieve a random range of numbers in Google Sheets by using the RANDBETWEEN formula. The formula is quite simple to use. Just add your start range and your end range:

=RANDBETWEEN(start range, end range)

For example, if I want to get a random number between zero(o) and 60 I would do the following:

=RANDBETWEEN(0,60)

The result will provide a whole random number somewhere between these two and including these numbers.

If I were to repeat this formula over a reasonable sample, say 300 times, we should see a fairly even spread of numbers occurring a roughly the same frequency, just like in the graph below:

But what if we wanted to have fewer random numbers in our list at the lower or upper end or on both ends?

Continue reading “Skewed Random Range in Google Sheets (RANDBETWEEN)”

How do I Randomly Assign People to a Group Without Doubling Up on the Same Group in Google Sheets

Random Combinations Without Repetition Using Arrays, TRANSPOSE and Randomize Ranges

Say you have a list of  30 people and a 30 list of groups. After every half hour for six hours the people must change to another group, but they cannot go back to a group they have already been in.

How can we schedule 12 sessions where every person can randomly go to another group without going to the same group twice?

This is pretty much the same issue I face when scheduling my teachers to proctor exams randomly each quarter for different classes. The rules I need to follow are:

  1. No teacher should proctor the same class twice.
  2. Assignment to proctor a class should seem random.
  3. The teacher that teaches the class cannot proctor their own class.
  4. Each week has an exam over the term. The term runs for 8 weeks plus a final requiring a total of 9 proctor sessions.

Continue reading “How do I Randomly Assign People to a Group Without Doubling Up on the Same Group in Google Sheets”

VLOOKUP Left in Google Sheets with INDEX and MATCH

Ever been faced with an issue where you can’t move your column to the far left in order to Vlookup? I just came across this issue recently.

The Problem – Vlookup

I needed to add teachers to a list of students who had to move into their class for two tests because their teacher (Hanna John-Kamen) would be absent on the test day.

Sheet: Hannah 302-15. Students split into other class sections.
Sheet: Hannah 302-15. Students split into other class sections.

 

Essentially, I was splitting the students into the remainder of the classes for that session time. In this student list sheet (Hannah 302-15) I had allocated the students new rooms and each room had a new teacher.

I couldn’t change the teacher proctors test list sheet ( Q2U1 Teacher) because the teachers who reference it would get confused. Unfortunately the teachers name I wanted was in column B and the reference (The Speaking classroom) was in column I.

 

Just like Zoolander, I can't Vlookup Left
Sheet: Q2U1 Teacher. Just like Zoolander, I can’t Vlookup Left

I need to put my first Vlookup in M2 of the Hannah 302-15 sheet.  If I tried to Vlookup it might look like this: =VLOOKUP(L2,‘Q2U1 Teacher’!B:I,8,False). Where L2 is the class I am searching for in the Hannah 302-15 sheet. ‘Q2U1 Teacher’!B:I is the range where B is the name column and I is the Class number. is clearly wrong. Vlookup will not accept a negative reference (It’s stupid, I know). False: because the list was not sorted. 

So what to do.

Continue reading “VLOOKUP Left in Google Sheets with INDEX and MATCH”