I am looking for a formula that: on 6/14/19, lookups up "Ige" in another column and grabs the numerical value. Once numerical value found, keep ONLY that value indefinitely. These are odds for betting that are constantly changing, so I want to grab only those specific odds on that specific day and put it into the box next to the date.
I know how to setup vlookup, but not sure how to do it on a specific date and only keep the value from that date.
Requirement:
Copy data matching dates to another range.
Solution:
function copyData() {
//spreadsheet variables
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getActiveSheet();
//data variables
var raw = sh.getRange('W30').getDisplayValue();
var range = sh.getRange(71, 17, 6, 2); //row 71, column R, for 6 rows and 2 columns
var data = range.getDisplayValues();
var date = Utilities.formatDate(new Date(), ss.getSpreadsheetTimeZone(), 'M/dd/yy');
//loop through range
for (i = 0; i < data.length; i++) {
//if date on sheet matches today's date
if (data[i][0] === date) {
var val = raw.toString().replace(/\+|-/g,''); //remove '+' and '-' from data
sh.getRange(71+i, 18).setValue(val); //set value
}
}
}
Explanation:
The script will loop through your range and compare the date in the column to the current date, then if they match it'll copy the value to the correct row in your range.
You will need to change var range if you want to record any more than 6 days worth of data, this is easy enough though, just change the '6' to however many rows worth of data you want to keep.
Related
I have a project roadmap tracker where each of the columns corresponds to the first date of the week. I have it automatically highlight the column if that date matches any date of the current week but the columns are so narrow that it's not easily legible. I want to expand the column that contains the a date that matches the current week when the sheet is opened.
I am not familiar with Google Apps script and have no idea how to approach this problem. Plus, hobbling together other example scripts have proven fruitless since I hardly know what these functions do with each other.
For highlighting the column of the current date/week, I'm using this conditional formatting formula that I would ideally prefer to be integrated into a script.
=AND(WEEKNUM(TODAY(),2)=WEEKNUM($2:$2,2),YEAR(TODAY())=YEAR($2:$2))
Where the formula looks for the date of each column in row 2 and checks if the date matches any date within this current week.
In short, I trying to write a script that:
On open
The script looks for the column that matches a date that exists within the current week
Automatically adjusts the column to be a specific width (like 200)
Automatically highlight that column a specific color (like pale red)
Basically it could be something like this:
function main() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// get the dates from the first row of the sheet
var dates = sheet.getRange(1,1,1,sheet.getLastColumn()).getValues().flat();
// get current week and year
var today = new Date();
var current_week = Utilities.formatDate(today, ss.getSpreadsheetTimeZone(), 'w');
var current_year = today.getFullYear();
// get indexes of the columns with dates belong current week and year
var columns = [];
for (let i in dates) {
var date = new Date(dates[i]);
var date_week = Utilities.formatDate(date, ss.getSpreadsheetTimeZone(), 'w');
var date_year = date.getFullYear();
if (date_year == current_year && date_week == current_week) columns.push(+i + 1);
}
// change width of the columns to 200
columns.forEach(col => sheet.setColumnWidth(col, 200));
// change backgrounds of the columns to pale red
var last_row = sheet.getLastRow()
columns.forEach(col => sheet.getRange(1,col,last_row,1).setBackground('#ffaaaa'));
}
As the title suggests I have a cell (B3) that updates it's value hourly. At a certain time each day, let's say 10pm, I'd like a script to run automatically that copies the value (x) from this cell to a cell in the same row, that matches the column headed with today's date.
Here is the example image
today = 26.8.21 (d,m,y :australia)
Here is where I am at so far:
// Record history from a cell and append to next available row
function recordValue() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheetoffruit");
// set today's date
var Date = sheet.getRange("J1").setValue(new Date());
// look up date range
var daterange = sheet.getRange("C1:1");
// find the column position of today's date
if (date = daterange) {
var col = daterange.getColumn(); }
var value = sheet.getRange("B55").getValue();
var row = 55;
sheet.getRange(row, col).setValue(value);
}
I have previously been able to post the value of the ("b55") cell (not in this example) however, I cannot get it to match with the column with today's date.
This function write the xtra fruit value in the column with todays data in row 1 and the last line of the data set.
function recordValue() {
let col = {}
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheetoffruit");
const dhA = sh.getRange(1,1,1,sh.getLastColumn()).getValues()[0];
dhA.forEach((h,i) => {col[h] = i+1});
let xf = sh.getRange(sh.getLastRow(),2).getValue();
sh.getRange(sh.getLastRow(),col[Utilities.formatDate(new Date(),Session.getScriptTimeZone(),"dd-MMM-yyyy")]).setValue(xf);
}
You can run the function from a timebased trigger but make sure you pick a time the ends up in the correct date. Because the col object and dts determine where the column is based upon matching with the strings in row 1.
After you explain how to determine the row then we can try the below function to see what going wrong with the col object which determines which column to write into.
We are using a Google Script to import a Range from other Spreadsheet to another.
This helped us in the past but now the data is growing and we need to reduce the data that we import. (timeout problems)
We need to import the rows with a specific date on a specific column.
In this case, as you can see in the script below, we are importing cells from 'A1' to 'N last row' in the range variable.
What we need is that in the column 'H' from that range date is checked with something like "Date in column K >= Today()-90"
// iterate all the sheets
sourceSheetNames.forEach(function(sheetName, index) {
if (EXCLUDED_SHEETS.indexOf(sheetName) == -1) {
// get the sheet
var sheet = sourceSpreadSheet.getSheetByName(sheetName);
// selects the range of data that we want to pick. We know that row 1 is the header of the table,
// but we need to calculate which is the last row of the sheet. For that we use getLastRow() function
var lastRow = sheet.getLastRow();
// N is because we want to copy to the N column
var range = sheet.getRange('A1:N' + lastRow);
// get the values
var data = range.getValues();
data.forEach(function(value) {
value.unshift(sheetName);
});
}
});
To conditionally copy the only the rows that meet a criteria, you will want to push them to a new array if they qualify. This push would be added to your existing data.forEach() call:
...
var now = new Date();
var today = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
var kept = [];
var data = range.getValues();
// Add qualifying rows to kept
data.forEach(function(row) {
var colHvalue = row[7];
var colKvalue = row[10];
if( /* your desired test */) {
// Add the name of the sheet as the row's 1st column value.
row.unshift(sheetName);
// Keep this row
kept.push(row);
}
});
/* other stuff with `kept`, like writing it to the new sheet */
You'll have to implement your specific test as you have not shared how time is stored in column H or K (e.g. days since epoch, iso time string, etc). Be sure to review the Date reference.
I've solved this in the past by adding a new column in the spreadsheet which calculates n days past an event.
=ARRAYFORMULA(IF(ISBLANK(K2:K),"",ROUNDDOWN(K2:K - NOW())))
The core of the function is the countdown calculation. For instance, today is Thursday, March 1. Subtracting it from a date in the future like Sunday, March 4, returns a whole integer: 3. I can test for that integer (or any integer) in a simple script.
In your script, add a conditional statement before executing the rest of the function:
// ...
if(someDate === -90) {
// do something...
}
This way, you're just checking the value of a cell rather than doing a calculation in a helper function. The only change (if you want a longer or shorter interval) is in the conditional test.
I'm trying to do a few things with a piece of script but cannot get it all down.
In row 2 of my sheet I have a series of consecutive dates based on the first of each month (British notation) e.g 01/08/2016, 01/09/2016, 01/10/2016. I then have a formula in rows 14 and 15 which I would like to be fixed (copy / paste value) when today's date matches that in row 2.
I feel that I need to run a few things -
Schedule a script to run once per day to check if any value in row 2 is equal to today's date. If true then...
Copy / paste values of the numbers in rows 14 and 15 and the column where the date matches.
Maybe an index/match is needed to verify part 1 but I'm really in the dark on how to do it.
Thanks for any assistance.
I think that I've now managed to crack it. In row 1 I have put in a helper formula which checks the date in row 2 against today's date and puts "TRUEMONTH" in the cell if it matches. My clunky code is then as follows (it still needs some tidying up for the sheet refs etc) -
function ColCheck() {
var name = "TRUEMONTH";
var name1 = "PASTE_ME_AS_VALUE";
var range = SpreadsheetApp.getActiveSheet().getDataRange()
var values = range.getValues();
var range1 = SpreadsheetApp.getActiveSheet().getDataRange()
var values1 = range1.getValues();
for (var row in values) {
for (var col in values[row]) {
if (values[row][col] == name) {
for (var row1 in values1) {
for (var col1 in values1[row1]) {
if (values1[row1][col1] == name1) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var row = parseInt(row1) + 1;
var col = parseInt(col) + 1;
var source = ss.getActiveSheet().getRange([row],[col]);
source.copyTo(ss.getActiveSheet().getRange([row],[col]), {contentsOnly: true});
}
}
}
}
}
}
}
I need to put the current date & time (on an individual row basis) in column K of my google docs spreadsheet, if either of column I or J is changed. This is my first attempt at using google scripts.
I've located a function that will do the time (and I'm hoping I can figure out how to do the date), but I don't know how to check for changes in cells, and update other cells. So for example if row 10 column I or J changes, row 10 column K should get the current date & time.
function myFunction() {
var d = new Date();
var timeStamp = d.getTime(); // Number of ms since Jan 1, 1970
// OR:
var currentTime = d.toLocaleTimeString(); // "12:35 PM", for instance
}
You can give this a shot - it uses string formatting to put the current date in the proper format (you can adjust as you need). One important thing to note: regardless of what you do here, the value will be displayed according to whatever formatting you have specified in the spreadsheet itself. Therefore if you don't do anything, the result is going to look like the traditional datetime representation. If you want it to appear in the format you specify, you can do two things: 1.) convert the entire column to text using the spreadsheet's Format menu; or 2.) (my choice) prefix the value you write with a ', which will essentially turn it into a string (that is what is done in the code below). Hope this helps.
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
// This was "MySheet" for me - change to whichever sheet you want to test
if( s.getName() == "MySheet" ) {
var r = s.getActiveCell();
// Check if the column is either I or J
var currColumn = r.getColumn();
if( currColumn == 9 || currColumn == 10) {
// If so, set dateCell = the current row at column J
var dateCell = s.getRange(r.getRow(), 11);
// Format the current date into datetime format (adjust to display how you want)
var dateTime = Utilities.formatDate(new Date(), "GMT-8", "MMM-dd-yyyy h:mm a");
// Set the cell
dateCell.setValue("'" + dateTime);
}
}
};