I want to create a macro that finds the leftmost empty cell in a row, then adds today's date to that row (as in setFormulaR1C1('=TODAY()') )
I have been able to get the last empty row or column from a sheet but not, specifically, the last empty cell from the row I'm standing on.
From I want to create a macro that finds the leftmost empty cell in a row, then adds today's date to that row (as in setFormulaR1C1('=TODAY()') ), I believe your goal is as follows.
You want to find the most left empty cell in the active row and want to put a formula of =TODAY() to the cell.
In this case, how about the following sample script?
Sample script:
Please copy and paste the following script to the script editor of Spreadsheet and save the script.
function myFunction() {
const sheet = SpreadsheetApp.getActiveSheet();
const range = sheet.getActiveRange();
const row = range.getRow();
const values = sheet.getRange(row, 1, 1, sheet.getLastColumn()).getDisplayValues()[0];
const column = values.findIndex(c => !c);
if (column == -1) return;
sheet.getRange(row, column + 1).setFormula("=TODAY()");
}
When this script is run, please select a cell in the sheet and run the script. By this, a formula of =TODAY() is put into the most left empty cell of the selected row.
When the empty cell is not found, the script is stopped.
If you want to fix the value of =TODAY() as the constant value, please modify it as follows.
From
sheet.getRange(row, column + 1).setFormula("=TODAY()");
To
const today = new Date();
today.setHours(0, 0, 0, 0);
sheet.getRange(row, column + 1).setValue(today);
References:
getActiveRange()
findIndex()
Related
Complete Google Apps Script novice question. I am trying to build some additional functionality into my spreadsheet where I can attach a script to a button that "checks off" the current selection as completed with the current date on a different sheet.
The pages are on one page of the sheet "sheet1" and the index of all pages are on "sheet2".
I am trying to run a textfind/replace function to find the cell of the match from sheet1, match the cell value from a column in "sheet2". From there I was thinking to try and pull out the row and column of that cell and then try write the date one cell to the right.
Im not sure how to pull the row and column out of the .getA1notation() below... and then really not sure if this is the right way to approach the problem.
From below I would want to look up the cell values from sheet 2 (F1143 on this one) and then write the current date in G1143.
Here is where I am so far:
function markComplete() {
var spreadsheet = SpreadsheetApp.getActive();
var encounterSheet = spreadsheet.getSheetByName("sheet2");
var tosearch = "chapter1"; //hoping to make cell reference from "sheet1"
var tf = encounterSheet.createTextFinder(tosearch);
var all = tf.findAll();
for (var i = 0; i < all.length; i++) {
Logger.log('The sheet %s, cell %s, has the value %s.', all[i].getSheet().getName(), all[i].getA1Notation(), all[i].getValue());
Logger.log(all[i].getA1Notation());
}
}
Logger results:
8:41:33 AM Notice Execution started
8:41:34 AM Info The sheet Sheet2, cell F1143, has the value Chapter1.
8:41:34 AM Info F1143
8:41:34 AM Notice Execution completed
Sheet.findAll() returns an array of the ranges containing that text.
let all = tf.findAll();
all.forEach( range => range.offset(0,1).setValue(new Date());
Reference
TextFinder.findAll()
Range.offset()
I have the following code as a starting point. I want to select the entire column where row 3 contains a specific date value (it will be the date of the previous Monday; I have a formula returning this date in cell E1).
function selectDate() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
ss.getRange(3,?,1,ss.getMaxColumns()).activate();
}
Basically, the getRange column value would be interpreted as something like: "Find the column number where the value in row 3 is equal to the value in cell E1".
Any ideas would be very helpful, even if it's using a totally different method to achieve the same thing. Thank you so much!
In your situation, how about the following sample script?
Sample script:
function selectDate() {
var sheet = SpreadsheetApp.getActiveSheet();
var searchValue = sheet.getRange("E1").getDisplayValue();
var res = sheet.getRange(3, 1, 1, sheet.getLastColumn()).createTextFinder(searchValue).matchEntireCell(true).findNext();
if (!res) return;
var column = res.getColumn();
sheet.getRange(1, column, sheet.getLastRow()).activate(); // Here, the found column is activated.
Browser.msgBox("Found column number is " + column); // Here, the found column number is shown in a dialog.
}
From your situation, I thought that getRange(3,?,1,ss.getMaxColumns()) in your script might be getRange(3, 1, 1, sheet.getLastColumn()).
When this script is run, row 3 is searched using the value of cell "E1". When the value is found, as a sample, the found column is activated and the column number is shown in a dialog. This is a sample. Please modify this for your actual situation.
Note:
If no column is selected, it is considered that the value of cell "E1" is not found in row 3. At that time, can you provide the detail of your Spreadsheet? By this, I would like to modify it.
Reference:
createTextFinder(findText) of Class Range
Google sheets / Google app's script
I'm want to know how to get a Cell, based in my current active Cell. Example:
A table where my active Cell is at G15, im wanna get the value of C15 (same row, but C column) and the value of C3 (same column now). To get the logic, imagines if my current Cell change to (example) H7, so, i would get C7 and H3.
I know It maybe is very simple, but im learning, so, im don't very very good ate that.
Try to add into the cell G15 this formula:
=TEXTJOIN(", ", TRUE, $C15, G$3)
And then copy it from G15 to H7, etc.
You can also achieve this using Apps Script:
function returnVals() {
let sheet = SpreadsheetApp.getActiveSheet();
let activeCell = sheet.getActiveCell();
let row = activeCell.getRow();
let col = activeCell.getColumn();
let colCValue = sheet.getRange(row,3).getValue();
let sameCol = sheet.getRange(3,col).getValue();
console.log(colCValue);
console.log(sameCol);
}
The getActiveCell method will return the active cell from the sheet and the getRow and getColumn will get the row and the col of the active cell. Afterwards, based on the row and col retrieved, the function above will simply get the values you need by using the getRange and getValue methods.
So if the active cell is G17, the row will be 17 and the col will be 7; therefore:
colCValue will be the value corresponding to the (17,3) range which is C17;
sameCol will be the value corresponding to the (3, 7) range which is G3.
Reference
getActiveCell();
getRow();
getColumn();
getRange();
getValue().
I have a Google sheet trigger that splits the values in Column F when any value in that column gets edited. However I want the script to only split the 'last row', and not all the rows (even the ones above already split)
Which line of code should I add to have the script only split the last edited row in column F and not the entire range? This is my sample so far:
function Split() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('F2:F50').activate();
spreadsheet.getRange('F2:F50').splitTextToColumns();
spreadsheet.getRange('F2:F50').splitTextToColumns(SpreadsheetApp.TextToColumnsDelimiter.COMMA);
};
Kind regards,
Brendon
I believe your goal as follows.
You want to reflect splitTextToColumns(SpreadsheetApp.TextToColumnsDelimiter.COMMA) to the column "F" of the last row of the active sheet.
In this case, you can use getLastRow().
Modified script:
function Split() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('F2:F50').activate();
// spreadsheet.getRange('F2:F50').splitTextToColumns();
// spreadsheet.getRange('F2:F50').splitTextToColumns(SpreadsheetApp.TextToColumnsDelimiter.COMMA);
spreadsheet.getRange("F" + spreadsheet.getLastRow()).splitTextToColumns(SpreadsheetApp.TextToColumnsDelimiter.COMMA);
}
Reference:
getLastRow()
I have a Google Sheet where I collect responses and another tab where I see a report for each record.
I want to add manually a number in the cell Writing Points (as in the image below) and click Update to update the cell of that specific record in the responses tab.
I managed to get the row number depending on the student the formula is:
MATCH($D$3,Responses!D:D, 0)
And the column is always BG of the repsonses tab.
How can I achieve this through Google script? I have an idea but I don't know how to do it.
My try:
row = MATCH($D$3,Responses!D:D, 0)
SpreadsheetApp.getActiveSheet().getRange('BG'+row).setValue(newwritingpoints);
I don't know how to conver the match formula into Google script syntax and how to copy and paste the value from that cell to the responses tab.
Google Sheet link:
https://docs.google.com/spreadsheets/d/1dE6UVABhVp7WqEFdC0ptBK2ne6wEA6hCf0Wi6PxcMWE/edit#gid=2032966397
I believe your goal as follows.
When Update() of Google Apps Script is run by the button, you want to retrieve the cells "D3" and "H24" in the sheet Report.
You want to search the value of "D3" from the column "D" in the sheet Responses.
When the value of "D3" and the values of column "D" in the sheet Responses are the same, you want to put the value of "H24" to the same row of the column "BG" in the sheet Responses.
For this, how about this answer? The flow of this sample script is as follows.
Retrieve the values from the cells "D3" and "H24" in the sheet Report.
Retrieve the values from the cells "D2:D" in the sheet Responses.
Create the range list from the retrieved values.
Put the value using the range list.
Sample script:
function Update() {
var spreadsheet = SpreadsheetApp.getActive();
// 1. Retrieve the values from the cells "D3" and "H24" in the sheet `Report`.
var reportSheet = spreadsheet.getSheetByName("Report");
var searchText = reportSheet.getRange("D3").getValue();
var writingPoints = reportSheet.getRange("H24").getValue();
// 2. Retrieve the values from the cells "D2:D" in the sheet `Responses`.
var responsesSheet = spreadsheet.getSheetByName("Responses");
var values = responsesSheet.getRange("D2:D" + responsesSheet.getLastRow()).getValues();
// 3. Create the range list from the retrieved values.
var ranges = values.reduce((ar, [d], i) => {
if (d == searchText) ar.push("BG" + (i + 2));
return ar;
}, []);
// 4. Put the value using the range list.
responsesSheet.getRangeList(ranges).setValue(writingPoints);
}
In this script, it also supposes the case that the value of "D3" might find at several rows in the sheet Responses.
References:
reduce()
Class RangeList