Please Forgive me if this has been covered couldn't find it. But Does anyone have an idea on how to get a weekday (monday, Tues, Ect...) in this Script i have running.
function newColumn() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Song of the Day');
sheet.insertColumnAfter(1);
sheet.getRange("B1").setValue(new Date()).setNumberFormat('mmm/d/yyyy');
}
Currently ^ It makes a new column B and adds the date in cell B1 Every day I would just like that date to remain the same but also display the day of the week beside it also Thanks in advance!
Display Day of Week:
function DisplayDayOfWeek() {
Logger.log(["Sun","Mon","Tue","Wed","Thu","Fri","Sat"][new Date().getDay()])
}
If I understand right you want Column B to contain the date, and you want that to format like 'mmm/d/yyyy' and then you want column C to also have the date but to be formatted to show the Name of the Day.
I would modify your script to place the date unformatted into both columns B and C, and then use the formatting controls in your spreadsheet to display them how you want to.
Your code would change to:
function newColumn() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Song of the Day');
sheet.insertColumnAfter(1);
sheet.getRange("B1").setValue(new Date());
sheet.insertColumnAfter(2);
sheet.getRange("C1").setValue(new Date());
}
On Your spreadsheet choose the Column C, then from Format>Custom Date and Time choose one with the Name of the Day listed, and remove the parts you don't want until it looks like this:
custom date image "Day Name Only"
If you wanted to format it with code then as shown here Javascript in Google Sheets script: help using setNumberFormat
you should grab the cell and format the cell:
var cell = sheet.getRange("B1");
cell.setNumberFormat('mmm/d/yyyy');
var cell = sheet.getRange("C1");
cell.setNumberFormat('dddd+');
Date number Formattings found here: https://developers.google.com/sheets/api/guides/formats
Related
I see lots of for (var i + code written but I have never been able to understand it. I can set a formula but I dont know how to manipulate it.
My current struggle is that I want my script setFormula(=A1+B2+X) to work by grabbing a value and minusing 1 from it. As I understand it would probably end up as setFormula(=A1+B2+" + i + ")"
In my specific case, X can either be 1 of the 2 following (ideally the 2nd one) to be 24 (by getting the value of 25 and then minusing 1 from it). The date on the sheet is the 25th of the month is in cell A1, and the tab (sheet) name is xxx25. I need to be able to have X as 25-1: we want to end up with it printing =A1+B2+24 into the cell.
1: Day(A1)-1 (if the day in A1 was 25th of a month, this would give us the result of 24)
So in cell A1 on the current sheet it has DD MMMM YYYY so lets say if its the 25th of a month and year, it will set the formula in the sheet as =A1+B2+24
2: The tab (sheet) name is xxx25. This tab will always have 3 letters followed by the day of the month.
(In my case I have 1 for each day of the month, such as xxx1 xxx2 .. xxx9 xxx10. If this needs to be changed on my side to xxx01 xxx02 .. etc I can do that if required.)
So I guess this method would grab the last 2 values of the sheetname (the date, which in the example is 25th) so it extracts(?) it from xxx25 and now remembers 25, minuses 1 from it, and places it into the formula to become =A1+B2+24
Thanks, please let me know if any explanation more is needed.
Will this be a user-executed function that runs in the currently open sheet? If so, you could grab the active sheet's name and extract all but the first 3 characters from it:
function myFunc() {
let sheet = SpreadsheetApp.getActiveSheet();
let cell = sheet.getRange('C1'); // Replace with the desired cell
let sheetName = sheet.getName();
let onlyDate = sheetName.substring(3);
cell.setFormula(`=A1+B2+${onlyDate-1}`);
}
Using the value of A1:
Here I created a script that will create a custom menu. Whenever the menu is clicked, it will get the active range, use it to get the current sheet, get the value of A1, parse it and use its value in setFormula.
Code:
function onOpen() {
/*
This function will add a custom menu in Google Sheets
named Set Formula and will execute setFormula function
*/
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Set Formula', 'setFormula')
.addToUi();
}
function setFormula() {
var range = SpreadsheetApp.getActiveRange(); //get the active range
var sheet = range.getSheet(); //get the sheet of active range
var date = sheet.getRange('A1').getDisplayValue(); //get the value of A1
var day = date.split(" ")[0] //split using space and get the first element of array
range.setFormula(`=A1+B2+${day-1}`); //set formula to the active range
}
Demo:
References:
Custom Menu
I am learning coding in Sheets, and have already jigsawed together a couple of games (based off very limited coding knowledge. Very fun to see it come together
Next task:
I am logging my hours spent doing "Growth" things (like learning, exercise, work etc.) vs "Leisure" things (like TV, games, FB scrolling etc). Up until now I just scroll down to today's date and add in the numbers manually. Would be fun to have a button at the top of my sheet that just adds hours to the "Growth" or "Leisure" cells for today.
I have used formulae in the cell G371 to reveal the A1 notation of my target cell, hoping I can use this info in a script to write to that cell:
=substitute(Cell("address",vlookup(A1,A2:D367,3)),"$","")
Where cell A1 contains =today()
and A2:A367 are all the dates in the year.
and columns C and D hold numbers for growth and leisure respectively.
Then I tried this code to try to see how to write to a cell. I realise I am probably missing some very fundamental knowledge...
var ss = SpreadsheetApp.getActiveSpreadsheet();
var cellname = ss.getRange('G371') // makes cellname refer to G371
var input = cellname.getValue() // makes 'input' return the contents of G371, in today's case its 'C120'
var test = ss.getRange(input) // hopefully reads 'C120' instead of "input"
test.setValue(1) // setting value to 1 for now, as I'm just testing cell
// referencing. later I will work on figuring out the
// math for the buttons. I'm not there yet.
So it didn't work. apparently the problem is with ss.getRange(input) but I don't know what to do about that.
So my question is how to write to a cell that is named in another cell?
hope it makes sense
Thanks!
PS Here is a copy of the sheet
https://docs.google.com/spreadsheets/d/1KkoCb8kY1XICMeB9bx65HXsldCS-fa75xJCaU52WGg8/edit?usp=sharing
The formula in the cell might be messing with the value you retrieve. To make sure that does not happen, you can use getDisplayValue() instead of getValue().
Another way:
Also, if you just want to retrieve the cell where to write today's growth (or leisure), you don't need to follow this roundabout way of creating a formula in another cell to reference this one. You can do this instead (check comments):
function todayGrowthCell() {
var sheet = SpreadsheetApp.getActiveSheet();
var date = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "d/M"); // Format today's date as in spreadsheet, in order to compare
var firstRow = 2; // Row where days start
var column = 3; // Column C (Growth)
var dates = sheet.getRange(2, 1, 366).getDisplayValues().map(row => row[0]); // Retrieve columns with dates (its display value, in order to compare with today's date, which is formatted accordingly)
var row = dates.indexOf(date) + firstRow; // Today's row
var cell = sheet.getRange(row, column); // Today's growth cell
cell.setValue(1); // Set value to today's growth cell
}
function archivr() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName('Página1');
sh.getRange('A2').setValue(Utilities.formatDate(new Date(), "GMT-3", "YYYY-MM-DD"));
}
I'm trying to use this script format to put today's date in my spreadsheet, but when I try to put it, the result is:
2020-02-52
Day 52? There is no logic to this, I don't know what's going on.
Additional, how can I also add tomorrow's date in A3?
As written in the official documentation, D is Day in year. Use dd instead.
I am writing a script that pulls events from Google Calendar to Google sheets for a singular date. I want the user to be able to type the date on the spreadsheet (in this case, in the cell F1), and then when you run the script, the script gets all the events for the relevant date. However, with my current code, I can only type in the date in the script editor, as the script won't run when I try to reference F1 in lieu of the written-out date. What am I doing wrong?
function getEvents() {
var ss= SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cal = CalendarApp.getCalendarById("email#email.com");
var events = cal.getEventsForDay(new Date("2/5/2020"));
for(var i = 0;i<events.length;i++){
var title = events[i].getTitle();
Logger.log(title);
ss.getRange(i+2, 1).setValue(title);
}
}
In short, I'd love to reference a cell in line 5, rather than the string of the date.
Sorry if this is so simple, I've never coded before so this is all new to me! Thank you for any help!
You want to retrieve the titles of events retrieving with getEventsForDay(Date).
You want to retrieve Date of getEventsForDay(Date) from the cell "F1" on the active sheet in the Spreadsheet.
You want to put the titles to the column "A" of row 2 on the active sheet.
You want to achieve this using Google Apps Script.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Modified script:
When your script is modified, please modify as follows.
From:
var cal = CalendarApp.getCalendarById("email#email.com");
var events = cal.getEventsForDay(new Date("2/5/2020"));
To:
var inputtedValue = ss.getRange("F1").getValue();
var cal = CalendarApp.getCalendarById("email#email.com");
var events = cal.getEventsForDay(inputtedValue);
When the value of the cell is the Date object, getValue() can be retrieved the value as the Date object.
If the value of cell "F1" is not the Date object, an error might occur. In that case, can you provide the sample Spreadsheet? By this, I would like to confirm it.
References:
getRange(a1Notation)
getValue()
How do I set a column to display the name of the month instead of the whole date using the script?
The function can manually be performed by selecting Format>Number>August in the menu but I want to do this in code.
This is what I've tried but I can't seem to find what I'm supposed to put in as the "format condition":
function myFunction() {
var spr = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spr.getSheets()[0];
var column = sheet.getRange("A:A");
column.setNumberFormats("Month"); // I'm pretty sure it's not supposed to be "Month" here, so what is it supposed to be?
}
Clarification; I want to set an entire column to change: 01/01/2015 to show January.
The error message I get on above code is "Cannot find method setNumberFormats(string)" and it's the same if i change "Month" to "L" or "MM".
A lot of typos in your script...(please be more careful when writing code and make use of autocomplete to check spelling : control+SPACE)
code goes like this :
function myFunction() {
var spr = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spr.getSheets()[0];
var column = sheet.getRange("A:A");
column.setNumberFormat("MMMM"); // MMM for abbreviated month, MM for month number
}