Overview
I have 2 Tiller sheets, one for Investments
and one for Budgeting
. I would like to have the balances of my Investments
sheet show up in my Budgeting
sheet, but do not want to add them as accounts, and get all of their many transactions cluttering up my main Budgeting
sheet.
So this is a script to get just those balances imported daily into my Budgeting
sheet.
This was inspired by a question from @stevation in this thread.
Also, @brettanicus made a template/formula based solution here. That would be a good fit if you donât want to mess with Apps Script, and donât mind a little manual work.
Installation & Setup
If this is your first time using Apps Scripts, check out the Beginner Installation & Setup Tips
It is a Google Apps Script function.
- Copy and paste the code into any *.gs file within âApps Scriptâ from the Extension menu in your Budget sheet.
- Set Variables
- Save
- Run the
investmentBalancesToBudgetSheet()
function - Set a Trigger to run on your desired frequency
Set Variables
There are a couple of things to update in the script so that it will work for your sheets.
Set a Trigger
Usage
This runs automatically at your trigger frequency. No manual actions needed.
Permissions
Is it ok for others to copy, use, and modify your workflow?
Yup.
The Code
Code is here on GitHub.
Also a static version:
function investmentBalancesToBudgetSheet() {
// ========================================
// Must Review and Update These Variables
// ========================================
var sourceSpreadsheetId = "aY2Lrvvq8BYrxjyD8EhltKpoQB6bO1NMRkgmAGaTjHRW"; // Change this to your source sheet's (Investment Sheet) ID
var accountColumnIndex = 3; // Column D (3) is "Account" column on my sheet. Update so the number is for the Account column on your sheet. (zero-based index: A=0, B=1, C=2, D=3, etc.)
// ==============================================
// Less likely to need changes to these variables
// ==============================================
var sourceSheetName = "Balance History"; // Source spreadsheet (Investment Sheet)
var targetSheetName = "Balance History"; // Destination sheet (Budget Sheet)
// ==============================================
// Script Logic - No changes needed below this point
// ==============================================
var sourceSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetId);
var sourceSheet = sourceSpreadsheet.getSheetByName(sourceSheetName);
var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(targetSheetName);
if (!sourceSheet || !targetSheet) {
Logger.log("One or both sheets not found!");
return;
}
var data = sourceSheet.getRange(2, 1, 500, sourceSheet.getLastColumn()).getValues(); // Read first 500 rows (starting from row 2)
var uniqueAccounts = new Set();
var filteredRows = [];
// Iterate through rows and store unique accounts
for (var i = 0; i < data.length; i++) {
var accountName = data[i][accountColumnIndex]; // Use configurable column for "Account"
if (!uniqueAccounts.has(accountName)) {
uniqueAccounts.add(accountName);
filteredRows.push(data[i]);
}
}
var numNewRows = filteredRows.length;
if (numNewRows === 0) {
Logger.log("No new unique rows to insert.");
return;
}
// Step 1: Insert new rows between row 2 and 3 (some conditional formatting or array formulas may not like row 2)
targetSheet.insertRowsBefore(3, numNewRows);
// Step 2: Copy row 2 and paste it to the last new empty row
var row2Values = targetSheet.getRange(2, 1, 1, targetSheet.getLastColumn()).getValues();
targetSheet.getRange(2 + numNewRows, 1, 1, row2Values[0].length).setValues(row2Values);
// Step 3: Paste new unique account data into row 2
targetSheet.getRange(2, 1, numNewRows, filteredRows[0].length).setValues(filteredRows);
Logger.log("Successfully inserted " + numNewRows);
}
Notes
Make sure both Balance History sheets have the same columns in the same order.
âHow do I get the ID for my Investment sheet?â
It is in the URL bar when you are in that sheet:
Beginner Installation & Setup Tips
<--- Click the Arrow for Install & Setup Tips
-
In Budget sheet, we are going to âpullâ data from your Investment sheet.
-
So from the Budget sheet, open the Extensions menu, and select Apps Script.
-
In either the default Code.gs file, or one you create, add all the code from above.
-
Set Variables
a. Make sure to get and set the ID for your Source sheet (Investment Sheet). It should go between the set of double quotes after thesourceSpreadsheetId =
b. Review what column is your âAccountsâ column, and set theaccountColumnIndex =
accordingly. -
Save (The button may say Save project to Drive)
-
Run the
investmentBalancesToBudgetSheet
function:
-
If this is your first time using Apps Script, Google may pop up a window for authorization. There are many variations of how this process looks, but here are some examples.
a. It may look like this if your browser blocks it: (You can allow the Google popup)
b. It may take you straight to this:
c. Likely you will have to approve your âappâ that you just created. It will have your own email address in the âdeveloperâ section. Takes 2 clicks:
-
Check that the balances were added to your Budget sheet Balance History tab.
-
Set a Trigger to run on your desired frequency.
-
Close the Apps Script tab.