Date added/created column for a google spreadsheet - google-sheets

Is it possible to insert the date/time when a row is inserted into a google spreadsheet.
Please note this is different from the NOW() method, which will insert the current date time as your view the spreadsheet...
The main purpose of this is to turn the google spreadsheet into a 'google form' which can be sent out to collect feedback and will have a history of when responses were given

something like the below in a Google App Script? This should set the value of the a set column at the time the edit is made.
If you are unfamiliar with GAS, select "Tools"->"Script Editor" from Spreadsheet menu. Select "Blank Project", then wipe out what's there and replace with this. Save it (name is unimportant) and go back to your Spreadsheet to try an edit and see results.
function onEdit() {
var dateColNum = 6 //column F
var ss1 = SpreadsheetApp.getActiveSpreadsheet();
//set date in same row as edit happens, at fixed column
ss1.getActiveSheet().getRange(ss1.getActiveRange().getLastRow(), dateColNum, 1, 1).setValue(new Date())
}

Related

Search multiple criteria in a Google sheet and return latest date

I am making a new sheet. B2:D2 - Cuvee, Sweet Pea, Zurkle (strains),
A - Dispensary (https://docs.google.com/spreadsheets/d/11ele1sfanpaXYdC4Ng2eWZSPDCCliguEf_ndep_REYU/edit#gid=0)
I want to search another sheet (https://docs.google.com/spreadsheets/d/1SvVyTrHO9saAZrr9DGxrSUNjZr-Y1JfPdSuwng50QE8/edit#gid=0) for the following criteria:
A: Product (B:D above)
D: Dispensary, and
E: Date
and return the last/latest date in the new chart. i.e. return the last date a specific product was purchased by a specific dispensary into the new sheet.
I am new at this and can get the formula for searching the criteria, but do not know how to return the latest date.
Assume that your need to place data in a separate spreadsheets, here is the answer:
Place this formula in 'B3' of your 'Chart 2' spreadsheet.
Since this formula uses IMPORTRANGE() function to get your source data, you may need to Accept it to load data from other spreadsheet on the 1st run.
=LAMBDA(SOURCE,DISPENLOOKUP,PRODUCTLOOKUP,
LAMBDA(PRODUCT,DISPEN,DATE,
MAKEARRAY(COUNTA(DISPENLOOKUP),COUNTA(PRODUCTLOOKUP),LAMBDA(ROWINDEX,COLINDEX,
IFERROR(TEXT(MAX(
FILTER(DATE,SEARCH(INDEX(DISPENLOOKUP,ROWINDEX),DISPEN),SEARCH(INDEX(PRODUCTLOOKUP,COLINDEX),PRODUCT))
),"dd/mm/yyyy"),"")
))
)(INDEX(SOURCE,,1),INDEX(SOURCE,,2),INDEX(SOURCE,,5))
)(QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1SvVyTrHO9saAZrr9DGxrSUNjZr-Y1JfPdSuwng50QE8/edit#gid=0","Sheet1!A1:E"),"WHERE Col1 IS NOT NULL"),$A$3:$A,$B$2:$2)

How to use a value to search a tab and get a value from the new sheet in googlesheet

Hello i'm using google sheet as my second databse and in the main page called Companies i have a list of companies as shown below:
and i wrote a function that generate a new tab for every companie in the first column. here is what a tab looks like
my goal is in the companies tab under "Workers" i want to get the value of "Total workers" of each companie. the list of companies will be constantly growing so i thought about maybe a function that uses the value of the first column to search for the tab and then get the value of G2.
I am really new to google sheet and i would appreciate any help on how to solve this problem
SUGGESTION
You can try this sample script below with custom function named getTotalWorkers & then add it as a bound script to your Spreadsheet file:
UPDATED Script:
function getTotalWorkers(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var names = ss.getRange("Company!A2:A").getValues().filter(String); //get names of the sheets on column A
var res = [];
for(x=0; x<names.length; x++){
var data = ss.getRange(names[x]+"!G2").getValue(); //get the current cell G2 values on every sheet tabs
res.push([data]); //place all values to a tem[orayr array variable
}
ss.getSheetByName("Company").getRange(2,6,res.length,1).setValues(res); //add the values under the "Workers" column on Company sheet tab
}
Sample Demonstration
After saving the script from the Apps Script editor, place the updated getTotalWorkers function to a time-driven trigger:
The time driven trigger will auto populate the "Workers" F column cells every minute (based on my sample time-driven trigger configuration):

How to keep lock a cell value even if the condition changed?

please refer to the images below
Sheet1
sheet2
I have 2 sheets: sheet1 and sheet2, here's what I want to do: if sheet2!B37 = sheet1!B1, take the value of sheet1!B2, if not stay blank.
This part is easy, but what I want is that if I change the date in sheet1!B1 to 16 Mar 2021 and sheet1!B2 to 90, I want sheet2!C37 to stay 100 and sheet2!C38 returns 90.
One way is to copy the content of sheet2!C37 and paste it as value.
Reference: Convert formulas to values in Google Sheets
But I doubt that you would want to convert the formula into a value every time you need to change the condition. Hence, you might want to consider using Google Apps Script and run it using a button.
Sample Code:
function updatePrice() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
//Get Sheet1 object
var sheet1 = ss.getSheetByName("Sheet1");
//Get Sheet1 object
var sheet2 = ss.getSheetByName("Sheet2");
//Get Sheet1!B1 string value
var targetDate = sheet1.getRange("B1").getDisplayValue();
//Get Sheet1!B2 string value
var targetPrice = sheet1.getRange("B2").getDisplayValue();
//Get Sheet2 column B range
var dates = sheet2.getRange("B1:B");
//Create a textfinder and look for the target date (Sheet1!B1 value)
var textFinder = dates.createTextFinder(targetDate);
var firstOccurrence = textFinder.findNext();
if(firstOccurrence!=null){
//Date found
//Update price in column C
firstOccurrence.offset(0,1).setValue(targetPrice);
}
}
What it does?
Get the active spreadsheet using SpreadsheetApp.getActiveSpreadsheet()
Get the sheet object for Sheet1 and Sheet2 using Spreadsheet.getSheetByName(name)
Select the range for Sheet1!B1 and Sheet1!B2 using Sheet.getRange(a1Notation). Get the string value using Range.getDisplayValue()
Select the range for Sheet2!B1:B using [Sheet.getRange(a1Notation)]
Create a textFinder using Range.createTextFinder(findText)
Get the first occurrence of the date string that was being searched using TextFinder.findNext(). If date string has no match, return will be null. If date string has match it will return a Range of the matching cell
Matching cell will give you a range in column B, to set the price value move the range 1 column to the right by adding 1 column offset using Range.offset(rowOffset, columnOffset) and set the range value using Range.setValue(value)
Add A Google Sheets Button To Run Scripts
Add a Google Sheets button. You add a button via the Insert > Drawing menu.
Click Save and Close, this drawing gets added to your Google Sheet. You can right click on it to resize it or drag it around to reposition it.
To assign a script, Right Click the drawing and click the three little dots in the top right of the drawing and select Assign Script. Then type in the name of the function you want to run from your Apps Script code.
Output:
Note:
Please don't forget to click enter when editing your Sheet1 values before clicking the button. Apps Script cannot update Sheet2 when the cell to be read is still in edit.

How can i change "-infinity" to today's date in Google Data Studio or Google Sheets?

I've got a text field containing different dates and cells containing -infinity (meaning today's date).
How can I change -infinity to today's date in Google Data Studio or Google Sheets and how do I change the cell from text to date?
The image below contains the column with the dates and -infinity.
Apart from the formula already presented in the previous answer, you can also make use of Apps Script in order to achieve your task.
Apps Script version
You will have to go to Tools > Script editor and use the code below which loops through the A column and whenever it encounters an -infinity value, it replaces it with the current date.
function replaceInfinity() {
let ss = SpreadsheetApp.getActiveSheet();
let validUntil = ss.getRange("A2:A").getValues();
for (let i=0; i<validUntil.length; i++)
if (validUntil[i][0] == "-infinity")
ss.getRange(i+2,1).setValue(new Date());
}
Before
After
Reference
Google Apps Script.
Now that you have added a spreadsheet, I duplicated your raw data sheet and added a processing formula in B1:
=ArrayFormula({"valid_until V2";IF(A2:A="";;IFERROR(VALUE(REGEXEXTRACT(A2:A;"\S+"))+VALUE(REGEXEXTRACT(A2:A;"\s([^/./+]+)"));DATEVALUE("1/1/2050")))})
This one array formula produces all results for Column B, including the header (which you can change to whatever you like).
I processed your "infinity" dates to show as the real date January 1, 2050 — a date far enough in the future to essentially serve the same purpose as "no expiration."

How to get list of row that has value from a range of data based on column header as the criteria?

I have a range of data populated from a google form in sheet "Data".
I need to make a report (in sheet "Report") that shows the students attendance, based on name (from drop down list). The data that is blank is excluded in that report, as seen in column i to column k
desired result
I have shared the sheet here
I have tried to use filter
=filter(Data!B1:I20,Data!E1:E20<>"")
but still I cant find the way to change the "condition" in this formula to refer to the sheet Report A1.
what is the correct formula to achieve this?
Thanks you for your help.
Using Apps Script you will have to use the function getSheetByName(name).
Example:
// The code below logs the index of a sheet named "Expenses"
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Expenses");
if (sheet != null) {
Logger.log(sheet.getIndex());
}
Then to find from column E the rows that have value you would have to use the getRange(a1Notation) method:
// Get a range A1:D4 on sheet titled "Invoices"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getRange("Invoices!A1:D4");
// Get cell A1 on the first sheet
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A1");
Then you would have to iterate over that rows and get the rows where the E column has something.
Once you have that you can get the Report sheet and set the values there with setValues(values)
For more information on how the function you may need to use you have the reference of SpreadsheetApp and the overview on Extending Google Sheets

Resources