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.
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.
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.
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.
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:
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.
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.
The Bad Way
JavaScript
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
BAD - Go to each cell and see if it contains a value
and then fill in the background if it contains a dash or
zero
*/
functionbadWay(){
//Loop through each column and each row in the sheet.
for(i=1;i<lastColumn;i++){
for(j=1;j<lastRow;j++){
varcell=searchRange.getCell(j,i).getValue();
if(cell==="-"){
sheet.getRange(j+1,i+1).setBackground("#cc4125");
}elseif(cell===0){
sheet.getRange(j+1,i+1).setBackground("#e69138");
};
};
};
};
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
GOOD - Create a client-side array of the relevant data
search through the array and if there is a dash or zero,
then add the relevant background color.
*/
functiongoodWay(){
// Get array of values in the search Range
varrangeValues=searchRange.getValues();
// Loop through array and if condition met, add relevant
// background color.
for(i=0;i<lastColumn-1;i++){
for(j=0;j<lastRow-1;j++){
if(rangeValues[j][i]==="-"){
sheet.getRange(j+2,i+2).setBackground("#cc4125");
}elseif(rangeValues[j][i]===0){
sheet.getRange(j+2,i+2).setBackground("#e69138");
};
};
};
};
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.
BAD - Go to each cell and see if it contains a value
and then fill in the background if it contains a dash or
zero
*/
functionbadWay(){
//Loop through each column and each row in the sheet.
for(i=1;i<lastColumn;i++){
for(j=1;j<lastRow;j++){
varcell=searchRange.getCell(j,i).getValue();
if(cell==="-"){
sheet.getRange(j+1,i+1).setBackground("#cc4125");
}elseif(cell===0){
sheet.getRange(j+1,i+1).setBackground("#e69138");
};
};
};
};
/*
GOOD - Create a client-side array of the relevant data
search through the array and if there is a dash or zero,
then add the relevant background color.
*/
functiongoodWay(){
// Get array of values in the search Range
varrangeValues=searchRange.getValues();
// Loop through array and if condition met, add relevant
// background color.
for(i=0;i<lastColumn-1;i++){
for(j=0;j<lastRow-1;j++){
if(rangeValues[j][i]==="-"){
sheet.getRange(j+2,i+2).setBackground("#cc4125");
}elseif(rangeValues[j][i]===0){
sheet.getRange(j+2,i+2).setBackground("#e69138");
};
};
};
};
Looking to learn more about Google Apps Scripts in a more structured format? Udemyhas some great courses that can get your from the basics to a real Google Apps Script pro!
Got a more specific problem you need help with, but don’t have the time to develop the skills? Fiverr’s your best bet to find a skilled Google Apps Script professional to solve your problem quickly and cheaply. *
*The above affiliate links have been carefully researched to get you to what you specifically need. If you decide to click on one of these links it will cost you just the same as going to the site. If you decide to sign up, I just get a little pocket money to help pay for the costs of running this website.
OpenPyXl is a Python open library that allows you to read and write Microsoft Excel files. Specifically, the ‘*.xlsx’ file extension. It helps you to create programs to create and modify files and automate your processes in excel.
NOTE: This post requires that you have some knowledge of Python and the OpenPyXl library. The library also needs to be installed for you to use.
Quite often, I find that I have to work with ranges of data that I need to either copy and paste into a new file or files, or copy > modify > paste into files.
The OpenPyXl library allows you to look at every cell of a file and either copy it or modify it by using the openpyxl.worksheet.Worksheet.cell() method. This method allow you to access each cell by the row and column as a numerical value.
Note! Unlike everything else in coding, rows and columns start with one(1) and not zero(0).
To select whole ranges of our data we need to iterate through it by both row and column and then store that data in a list to be pasted to the new file, spreadsheet or location that we desire.
The following example will take you through the process. For your own data you will need to modify the file, sheet and range locations. Everything else should be good to go.
You can find the whole code at the end of the post.
Why does your MS Excel look weird?
To be honest my screenshots of the ‘.xlsx files will be in Libreoffice. But this simple example will be able to load without issue in MS Excel.
The Example
Source Data
The source data for this example is a very simplified version of grade data that I work with day-to-day. The goal of our program is to simply copy the Section 12 results into a new file. The file for this spreadsheet is called: GradeSample.xlsx. I have put it in the same folder as my Python program.
Receiving File
For the purpose of this example we have a file that we want to save this data into: Section12Grades.xlsx. We could just as easily use a template and save the file under a different name – for example template.xlsx could save as sec12Grade.xlsx. This could be useful if I wanted to save a file for each section.
The receiving file looks like this:
Loading the Source Data and Receiving File
Okay, let’s get started with the program. First we need to load both the source data and the receiving file.
Loading the Spreadsheets
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#! Python 3
# - Copy and Paste Ranges using OpenPyXl library
importopenpyxl
#Prepare the spreadsheets to copy from and paste too.
#File to be copied
wb=openpyxl.load_workbook("GradeSample.xlsx")#Add file name
sheet=wb.get_sheet_by_name("Grades")#Add Sheet name
#File to be pasted into
template=openpyxl.load_workbook("Section12Grades.xlsx")#Add file name
temp_sheet=template.get_sheet_by_name("Sheet1")#Add Sheet name
We import he OpenPyXl library first.
Next, we’ll open the source data with wb = openpyxl.load_workbook("GradeSample.xlsx") . Once we have loaded the workbook we need to tell Python which sheet tab we want it to work in. We do this by calling the workbook (wb) and then get the sheet by it’s name: sheet = wb.get_sheet_by_name("Grades")
We repeat this step with the receiving data file that we want to paste our Section 12 grades into.
Copying the Section 12 data
Looking at the section 12 data, we want Python to be able to copy from column A2 to D14. the OpenPyXl .cell() method takes a number which is much easier to iterate, rather than the column letters. So let’s look at that range again:
From: A2 is now column = 1 and row = 2
To: D14 in now column = 4 and row = 14
Once we access this data, we need somewhere to store it before we paste it into the new file. We will use a nested list for this.
Copy Range Data
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#Copy range of cells as a nested list
#Takes: start cell, end cell, and sheet you want to copy from.
#Adds the RowSelected List and nests inside the rangeSelected
rangeSelected.append(rowSelected)
returnrangeSelected
In line 3 we create our function copyRange. It contains 5 arguments that we could add with our information as follows:
startCol = 1
startRow = 2
endCol = 4
endRow = 14
sheet = sheet ( The name of the sheet variable we are copying the data from which is GradeSample.xlsxsheet Grades)
In line 4 we create an empty list called rangeSelected this list will have data from every row we have selected.
Line 5 starts the for loop through each rows. Each row contains data in each column so we create an empty list (rowSelected) here in preparation to add the column data to in the next for loop (Line 6).
Line 6 loops through each item in each column of the selected row. Line 7 then adds the data from each column to the rowSelected lists.
Once it finishes this loop, it adds the data from the rowSelected lists into the rangeSelected lists. It then moves down to the next row and repeats the process continuing through the rows until the loop meets it’s end at row 14.
Finally the copyRange function returns the rangeSelected list to be used at our discretion.
Pasting the selected data
Now we have a nested list of all the rows and the information in the columns we require from those rows.
We will use that list add it to our Section12Grades.xlsx in Sheet1.
We want to add the data starting at row 3 this time because we have a title on row 1 and column headers on row 2.
Line 3 starts our pasteRange function and contains the following arugments:
startCol = 1
startRow = 3 (because we want to paste the data 1 row down.)
endCol = 4
endRow = 15 (because we want to paste the data 1 row down.)
sheetReceiving = temp_sheet (This is the variable for Section12Grades.xlsx with the sheet name, Sheet1.
copiedData = rangeSelected ( The returned list from your copyRange function)
Line 5 creates a count number variable starting with zero(0) so we can start looking through our copiedData lists by rows from the beginning.
Line 6 begins the row loop like the copyRange function.
Line 7 adds another count number variable so we can loop through our copiedData list by columns.
Line 8 begins the column loop.
Line 9 adds the copiedData cell-by-cell to the new file. It does not save it here but holds it in memory to be saved to the file we choose in the future.
Finally, we add 1 to each of the counters so we can move onto the next cell.
Running a Copy and Paste as a function
We are now going to create a function to copy the data using the copyRange function and paste the data using the pasteRange function and then we will save the Section12Grades.xlsx file contained in the variable, template.
Line 1 creates the function and then line 3 runs the copyRange process with the arguments we need.
Note! After line 3 you could call a function to manipulate the data or add styling here before you paste it.
Line 4 then runs the pasteRange fuction with the arguments we need.
Line 5 then saves the pasted data in the same file that we used in our memory. You could equally save the file with a different name and this will create a brand new file with the pasted data in it.
That’s it! Done.
Run the program
Now it’s time to run the program. Save the file and hit run (F5).
In the Python Shell run the program and then enter:
Run the program.
Python
1
>>>go=createData()
Your result will look like this:
Result
Python
1
2
3
4
>>>go=createData()
Processing...
Rangecopied andpasted!
>>>
No too impressive, right?
Go into the folder and open your Section12Grades.xlsx spreadsheet.
It should now look like this:
The full OpenPyXl copy and paste range code
OpenPyXl Copy and Paste Range
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#! Python 3
# - Copy and Paste Ranges using OpenPyXl library
importopenpyxl
#Prepare the spreadsheets to copy from and paste too.
#File to be copied
wb=openpyxl.load_workbook("foo.xlsx")#Add file name
sheet=wb.get_sheet_by_name("foo")#Add Sheet name
#File to be pasted into
template=openpyxl.load_workbook("foo2.xlsx")#Add file name
temp_sheet=template.get_sheet_by_name("foo2")#Add Sheet name
#Copy range of cells as a nested list
#Takes: start cell, end cell, and sheet you want to copy from.
selectedRange=copyRange(1,2,4,14,sheet)#Change the 4 number values
pastingRange=pasteRange(1,3,4,15,temp_sheet,selectedRange)#Change the 4 number values
#You can save the template as another file to create a new file here too.s
template.save("foo.xlsx")
print("Range copied and pasted!")
Big thanks to WY in the comments for reminding me to update this.
Want to learn how to automate your daily admin at work with Python? Udemy has some great Python automation courses that will help you learn how to automate your tasks so you can focus on what really matters.
Got a more specific problem you need help with, but don’t have the time to develop the skills? Fiverr’s your best bet to find a skilled professional to solve your problem quickly and cheaply. *
*The above affiliate links have been carefully researched to get you to what you specifically need. If you decide to click on one of these links it will cost you just the same as going to the site. If you decide to sign up, I just get a little pocket money to help pay for the costs of running this website.
Spacer is a Free Google Add-On that creates row spaces by column categories.
I use it as an academic administrator to create a space or two at the end of each class section to create a visual break between sections. This makes it easier for my staff to quickly identify class sections.
Allow you to select a column with the categories you want to use to space your data.
Set the height of your header – so that is not spaced too.
Set the thickness of your spaces.
Set the color of your spaces
Set starting and end position of your color filler.
How to use Spacer
After Installation
Before You Start:
Before you start, create a Google Sheet with the data in it you want to space.
Select All the data.
Go to the menu bar and select: Data>Sort Range... and select the column you want to sort by. This will be the column you will use to space your data by the categories in that column.
Running Spacer:
In a Google Sheet, select the Add-Ons menu.
Select: Spacer>Create Spaces
A sidebar will appear to the right.
Choose the column you want to use to Space your data.
Choose how high your header is.
Chose how thick or deep your want your header to be.
If you want a color, select a color that you want to add to your spaces.
Select where you want to start the color and end the color.
Click: Submit
If you are not happy with the color, space, color width or made a mistake, simply click: UNDO
Troubleshooting
I keep getting the following popup:
This is because the starting value of your Color Width is greater than your End value:
An Example
I have a list of people with their favorite color and fruit. Let’s say I want to sort the Google Sheet by favorite color (Fav Color) and put a space at the end of each color category to make it easier to read.
My data looks like this:
To space this data we must do the following:
Select all the data: ctrl+shift + a
Sort the data by column B. Select: Data> Sort range
Click the: Data has a header row tick box. Select the column: Fav Color. Then click the Sort button.
Your data is now sorted by favorite color:
Time to use the Spacer. Select: Add-ons > Spacer > Create spaces
Spacer will open at Sidebar on the right:
We want to space our data by Fav Color which is column B. For Column, select: B
We have a single Row Header so we can leave the header selection at 1:
We want to give our spaces a thickness of 2 rows:
Next, we choose a color. Let’s choose purple. A color picker will pop up (the picker may vary depending on your system) select the color and click: OK:
This is the result:
Now choose where to start and finish the color fill for the space. I’ll start it at column A and end it at column E.
You sure can. Google has a fully supported script editor that you integrate with your Sheets, Docs, Forms, Slide, Gmail, Calendar and pretty much every aspect of the Google Suite. It’s name: Google Apps Script.
Google Apps Script allows you to do all sorts of things like building short code to modify sheets and docs, create macros, develop add-ons mess around with gmail and so much more.
Google Apps Scripts is based on Javascript. It can integrate with HTML5, CSS and Javascript well with it’s html service class.