I have huge sheets full of numbers - i'm looking to create a button in google sheets which allows me to jump to todays date (the dates are in a column)
i know how to create a hyper link however i want to click a button instead of text - google sheets only allows you to add script to a button and not hyperlinks.
Does anyone know how to script a button in google sheets to automatically jump to the current date on the sheet?
function gotoCurrentDate() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet Name');
const sr = 2;//data start row
const hA = sh.getRange(1, 1, 1, sh.getLastColumn()).getValues().flat();//might need to edit header row
const vs = sh.getRange(sr, 1, sh.getLastRow() - sr + 1, sh.getLastColumn()).getValues();
const dt = new Date();
const dts = Utilities.formatDate(dt,Session.getScriptTimeZone(),"MMM dd, yyyy");//might want to change format
const pos = { col: {}, idx: {} };
hA.forEach((h, i) => { pos.col[h] = i + 1; pos.idx[h] = i; });
vs.forEach((r,i) => {
let d = new Date(r[pos.idx['Date Column Header Name']]);
let ds = Utilities.formatDate(d,Session.getScriptTimeZone(),"MMM dd, yyyy");//might wat to change format
if(ds == dts) {
sh.getRange(i + sr, pos.col['Data Column Header Name'] ).activate()
}
});
}
You can goto to Google Apps Script Reference and using the search box find any function that you don't understand.
If it's a pure JavaScript function the go here
<input type="button" value="Go To Current Date" onClick="google.script.run.gotoCurrentDate();" />
Put this into the html of a sidebar and use below function to launch side break. When you open an html file it will come with some html put this into the body and save it.
If you get more than one input controls you may like to have this in the head.
<style> input {margin: 1px 2px 1px 0};
gs:
function launchSideBar() {
SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutputFromFile('sidebar html file name. Do not include the extension').setTitle('Sidebar Title'))
}
Here's what the side bar I use on the spreadsheet I answer questions on looks like today:
You can use buttons right on the spreadsheet but personally I think that they are a PITA.
Just in case, here is the lite version (with a custom menu instead of a button):
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('🛠️ Scripts')
.addItem('Jump to current date', 'jump_to_current_date')
.addToUi()
}
function jump_to_current_date() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('Sheet1');
var dates = sheet.getRange('A1:A').getDisplayValues().flat();
var date = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy/MM/dd");
var row = dates.indexOf(date) + 1;
if (row > 0) { sheet.getRange('A' + row).activate() }
else { ss.toast('No current date was found') }
}
The code searchs for dates in column 'A' of the sheet 'Sheet1' and supposes that dates are formatted as '2021/11/30'. To run the script it creates the 'Custom menu':
Of course you can draw a 'button' to fire the function jump_to_current_date(), if it suits you.