Google Sheets: Show values on a tab depending on date - date

We have a restaurant and want to show the Menu on a screen with google sheets.
I am using 2 tabs in Google sheets, The main (number 1) with the menu showing to the visitors, the second tab (number 2) has all the dates from today until coming 6 weeks like:
Column A
Column B
Column C
Column D
21 June
Appetizer this day
Main dish this day
Desert today
22 June
Appetizer this day
Main dish this day
Desert today
23 June
Appetizer this day
Main dish this day
Desert today
etc. etc.
Now what we want is that the 3 menu items are copied to the main tab on the day of today, so if it is 22 June, the corresponding appetizer, main dish and desert are copied to the main tab.
Is that possible?

This can be achieved with Google Apps Script. You can have a JavaScript code snippet that does this for you.
Open the required spreadsheet. Go to the Tools menu and click Script Editor. This will open up the script editor window with an empty function, code will have to be written inside this function, give it a name, I'm using updateFood():
function updateFood() {
// Fetch all the sheets/tabs of the current spreadhsheet
let sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
// Select the second sheet/tab using [1], first tab will be [0]
// Fetching the second sheet because that is where the data about the upcoming dates are present
let listSheet = sheets[1];
// Get all the data in the sheet
let data = listSheet.getDataRange().getValues();
// Iterate over all data in the sheet
for(i = 0; i < data.length; i++) {
// Get the date of the current row and create a date object
rowDate = new Date(data[i][0]);
// Get today's date
today = new Date();
// Check if the row date is equal to today's date
if(rowDate.setHours(0,0,0,0) == today.setHours(0,0,0,0)) {
// If the code reaches here, this row has the date same as today
// Get the first tab/sheet and then I'm selecting the cells A1, B1 and C1
// Modify the range as per which cells you want the dish names to be in
let range = sheets[0].getRange("A1:C1");
// Set the values of the selected three cells as the value of the second, third and fourth column in the current row of the 2nd tab which are Appetizer, Main dish and Dessert for that date respectively
range.setValues([[data[i][1], data[i][2], data[i][3]]])
}
}
}
And that's all the code you need! Save it and then you can try clicking on run code and then checking the first tab to see the values updated. However, when you click run for the first time, Google will ask you to grant permissions to the script to access your sheet. Click Review Permissions and then click on your account. Then click Advanced and then click:
.
But you will have to run this code every day. This can be achieved using a time-driven trigger.
In the same script editor window, look at the triggers button on the left:
Click on the + Add Trigger on the bottom right of the screen.
And select the values as follows (change the time as required):
That's all you need. Make sure that in the second tab, create dates in the date column with the Excel DATE(YEAR, MONTH, DAY) function rather than manually typing in the dates.

Related

Auto select an option from a dropdown after a certain date

I'm creating a lost&found log for work, I have a dropdown list with options like "Collected", "Sent to guest", "Donated" etc.
I'd like to have the sheet automatically select 'Donated' after a certain date (maybe after 1 month) IF an option hasn't already been selected.
E.G. If somebody inputs a found pair of gloves on Nov 2nd, The empty dropdown will automatically change to 'Donated' on Dec 2nd unless somebody already selected another option (E.G. 'Collected').
You can accomplish this by either making use of Apps Script to check the conditions and make the corresponding changes or by directly implementing Sheets functions.
Sheets Functions
You can achieve the desired outcome by putting this formula on each status cell:
=IF(TODAY()-D4>30,"Donated","")
It will work as intended, the cell will not display anything until there are 30 days between the TODAY() date and the date provided in the D4 cell. If there are, the cell will display Donated. If another option from the drop-down is selected, that will erase the formula and only the new value will be displayed.
However, this method is more of a quick hack than an actual robust solution, as several things can go wrong. For instance, if an option from the drop-down is chosen by accident and then it is left blank again, the method will not work anymore for that line as the formula will have been permanently erased.
You can read about how TODAY() works here.
Apps Script (recommended)
The following script will check that, for every row in the sheet, both the status cell is blank and that subtracting the DATE FOUND is more than 30 days (unfortunately that has to be done in milliseconds).
function myFunction() {
var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadSheet.getSheetByName("Sheet1");
sheet.getLastRow()
var range = sheet.getRange(4,1,sheet.getLastRow(), 7);
//SELECT A RANGE WITH THE 7TH FIRST COLUMNS AND AS MANY ROWS AS NECESSARY
//(WE ARE INTERESTED IN DATA STARTING FROM ROW 4TH)
var values = range.getValues();
var millisecondsIn30Days = 30 * 24 * 60 * 60 * 1000;
for(var row = 0; row<values.length; row++){
var status = values[row][6]; //STATUS IS THE 6TH ELEMENT IN THE ROW ARRAY (COLUMN G)
if(status == "" && (new Date() - values[row][3]) > millisecondsIn30Days){
//MORE THAN 30 DAYS AFTER FOUND
sheet.getRange(row+4,7).setValue("DONATED");
}
}
}
Of course, that script can be run manually or the job can be automated with an installable trigger that runs, for example, every month. You can read more about how to set up such triggers here.

Excel stops working when adding new row to the Table

I don't know why but when I try to do this Excel Quits without saving anything:
On Sheet1 I have 11 Rows x 6 cols formatted as Table. With 1st row as Table Header.
I assign col A as a STU_ID Named Range using Name Manager.
I have only a listbox on the Userform. And I give RowSource property as =STU_ID . I get the col A data on the list box.
Now when I go to Sheet1 and try to add more data on next row of the table. Excel quits saying it has stopped working. Windows is checking for error and it restarts on a blank Workbook.
Shouldn't Listbox be dynamic and get data from Name Manager as I add them on the sheet ?
I am using MS Excel Pro 13 on Win 10 64 bit.
This is a bug in Excel. As a workaround, use the .List property instead of the .RowSource.
Try something like this in the UserForm's code:
Private Sub UserForm_Initialize()
ListBox1.List = Range("table1[abc]").Value
End Sub
This way it will be dynamic, and it will work.
If you need it to change while the userform is displayed, put a small code to the Sheet change event, that updates the list every time the sheet changes. (This is only necessary if your form is modal, and you want it to reflect the changes made in real-time.)

Excel Form VBA Combobox reset

This should be easy but I can't seem to find anything to solve. I have a Form in Excel using VBA. The Excel sheet has four columns populated with data and the Form displays those fields.
The user then selects a value in a combobox, uses a command button to submit the responses and the values from the combobox are written to the Excel sheet.
The user then uses a command button to advance one row down in the spreadsheet and load the four values from a new row. This all works great.
The issue I am trying to solve is that the combobox remains selected to the value in the prior selection. I'd like to reset the combobox so nothing is selected and the user has to make a selection again for the next row.
Below is the code I am using to load the combobox and to set a variable for what the user selected. Can't seem to get the combobox back to it's default state after the user has submitted the form.
Private Sub cbDesAccWanted_Change()
Select Case cbDesAccWanted.Text
Case "Yes"
desacc = "Yes"
Case "No"
desacc = "No"
End Select
cbDesAccWanted.Text = desacc
End Sub
Private Sub cbDesAccWanted_DropButtonClick()
cbDesAccWanted.List = Array("Yes", "No")
End Sub
There are two ways to reset the combobox. Take your pick:
1
cbDesAccWanted.Value = Null
2
cbDesAccWanted.ListIndex = -1
the line
cbDesAccWanted.Text = desacc
is totally unnecessary.
Using cbDesAccWanted_DropButtonClick is not the right place to populate the list of values. This list should be set up when the form is first shown to the user.
(unelss the values it shows chnages in which case change it when the row changes or something, not when the user clicks on it)
So when theuser clicks the down arrow to move to thenext record include the following line
Me.cbDesAccWanted.text = Me.cbDesAccWanted.List(1)
Note (1) access teh 2nd item in the list which is No.
So this reset it to the default value of No.
Ok.

wicket DatePicker - show last select value

I am using org.apache.wicket.datetime.markup.html.form.DateTextField (part of wicket-datetime-6.9.0-sources.jar ) with wicket 6.9.0
I need the DatePicker to auto select the last selected value, when it is opened an empty value.
As it is, when I open the date picker with an empty value no date is selected.
Last selected value, means , lets say somone choose 1.1.2019 so the next time the datepicker instance will b opened from another date field the value would show 1.1.2019

Kendo DatePicker Max date issue

I have a two kendo DatePickers to select start and end date of a job. job consists of multiple tasks which contains its own completion date (tasks are listed in a grid with kendo DatePicker for each record to select the completion date)
I set max and min of each task when user sets the job start and end date. I use kendo to bind data with kendo (through kendo knockout).
problem is when user clears the end date of a job,I set the max date of task level DatePicker to (2099, 11.31), but when I click on the task level datepicker I cant navigate to next month at once. if i click on some other datepicker can navigate. this happens when I delete start or end date of the job level.
Well this question doesn't seem to be 'active' anymore but for reference, I managed to get around the problem by calling .enable() on the kendo control after setting the new value (I was using ko + ko-kendo but other than that it's exactly the same)
Fiddle: http://jsfiddle.net/AlexPaven/m5M46/2/
Code in fiddle:
var vm = {
val: ko.observable(new Date()),
mx: ko.observable(new Date())
};
ko.applyBindings(vm);
setTimeout(function() {
vm.mx(new Date(2099, 11, 31));
var d = $('#a').data('kendoDatePicker');
d.enable(); // commenting this exhibits the problem - max constraint isn't updated visually
}, 3000);
I'm reasonably sure this has no side effect; if you want to preserve the enabled state I'm sure you can check the state and call enable+disable or disable+enable.
Jeez, this was annoying.
EDIT: wrong, I was fooled by the behavior which is a little more involved. You only get the bug if you open the datepicker each time you set a new maximum; the first time the change is acknowledged but subsequent times it's not. I'll spend a few more minutes with this I suppose...