I am using the following code to automatically turn inputs in columns 1 and 4 of a sheet into a hyperlink. Whenever any range beyond 1 cell is pasted into the sheet only the first value will be hyperlinked into all cells in the range. Is there a way to get the cells within the range so I can have it check each cell for their individual value and column?
Thanks for any help!
function onEdit(e) {
// Set a comment on the edited cell to indicate when it was changed.
let range = e.range;
let order = range.getValue()
if (!( order === ''))
{
if (range.getColumn() == 1 || range.getColumn() == 4) {range.setValue("=hyperlink(\""+order+"\", \""+order+"\")");}
}
}
If you wish to tie it down to a single sheet
function onEdit(e) {
//e.source.toast('Entry')
//Logger.log(JSON.stringify(e));
const sh = e.range.getSheet();
if (sh.getName() == "Sheet1" && e.range.rowStart > 1 && (e.range.columnStart == 1 || e.range.columnStart == 4) && e.value) {
//e.source.toast("flag1");
e.range.setValue(`=hyperlink("${e.value}","${e.value}")`);
}
}
Related
I have the following script. Its full purpose is to remove cells of columns B,C,D,E,F if the content in column A was deleted, only on the matching row. It functions as it should. However, it is a bit slow.
I couldn't figure out a way to do it myself. Which is why I am here. How could I go about adjusting it, so instead of removing the cells individually, I could instead grab a full range of them, in the matching row? Perhaps based on the offset of the initially adjusted cell.
function onEdit(e) {
if(e.range.columnStart === 1
&& e.range.rowStart > 1
&& e.range.getSheet().getName() == 'Sheet1'
&& e.range.getValue() == '') {
e.range.offset(0,1).deleteCells(SpreadsheetApp.Dimension.ROWS);
e.range.offset(0,2).deleteCells(SpreadsheetApp.Dimension.ROWS);
e.range.offset(0,3).deleteCells(SpreadsheetApp.Dimension.ROWS);
e.range.offset(0,4).deleteCells(SpreadsheetApp.Dimension.ROWS);
e.range.offset(0,5).deleteCells(SpreadsheetApp.Dimension.ROWS)
}
}
To implement a couple of additional checks for copy-paste and the like, and to observe onEdit(e) best practices, try this:
/**
* Simple trigger that runs each time the user hand edits the spreadsheet.
*
* #param {Object} e The onEdit() event object.
*/
function onEdit(e) {
if (e.range.columnStart !== 1
|| e.range.rowStart <= 1
|| e.range.getSheet().getName() !== 'Sheet1'
|| (e.value || e.range.getValue())
|| e.range.getHeight() + e.range.getWidth() > 2) {
return;
}
e.range.offset(0, 1, 1, 5).clearContent();
}
Try:
function onEdit(e) {
if(e.range.columnStart === 1
&& e.range.rowStart > 1
&& e.range.getSheet().getName() == 'Sheet1'
&& e.range.getValue() == '') {
deletecols()
}
}
function deletecols(){
var sh = SpreadsheetApp.getActive().getActiveSheet()
var range = sh.getActiveRange()
var numrows = range.getNumRows()
var row = range.getRow()
sh.getRange(row,2,numrows,5).clearContent()
}
My not be that much faster:
function onEdit(e) {
const sh = e.range.getSheet();
if(e.range.columnStart == 1 && e.range.rowStart > 1 && sh.getName() == 'Sheet1' && e.value == '') {
sh.getRange(e.range.rowStart,2,1,5).setValues([['','','','','']]);
}
}
I am having 2 google sheet, having checkbox on 1st Sheet.
Requirement: When the checkbox is checked , Value from Cell C5 need to be stored in the Sheet2. When the checkbox is checked i.e value is 'True', the value in cell C5 of the Sheet1 needs to be submitted in the Sheet2 in the continuous manner.
I know this is the silly requirement, but I failed after several attempts as well.
Try
function onEdit(event) {
var sh1 = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if (sh1.getName() == 'Sheet1' && r.getA1Notation() == 'D5' && r.getValue() == true) {
var sh2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet2')
sh2.getRange('A' + (sh2.getLastRow() + 1)).setValue(r.offset(0, -1).getValue())
r.setValue(!r.getValue())
}
}
Store C5 in Sheet2 When Checkbox is checked
function onEdit(e) {
var sh1 = e.range.getSheet();
if (sh1.getName() == 'Sheet1' && e.range.columnStart == 4 && e.range.rowStart == 5 && e.value == "TRUE") {
var sh2 = e.source.getSheetByName('Sheet2');
sh2.getRange(sh2.getLastRow() + 1, 1).setValue(e.range.offset(0, -1).getValue());
e.range.setValue("FALSE");
}
}
I found this question and answer which was helpful but I'm wondering how to apply the script (also pasted below) to multiple tabs within a sheet. The column (column 2) to check for an edit to trigger the line to move to the bottom is going to be the same on all sheets (Sheet1, Sheet2, Sheet3).
function onEdit(e) {
const row = e.range.getRow();
const col = e.range.getColumn();
const as = e.source.getActiveSheet();
if(as.getName() == "Sheet1" && col == 2 && row > 1 && !as.getRange(row,col).getValue()=='') {
const row_new = as.getRange(row,1,1,col);
row_new.copyTo(as.getRange(as.getLastRow()+1,1,1,col));
as.deleteRow(row);
}
}
Thanks in advance!
Applying onEdit to multiple sheets
function onEdit(e) {
const sh = e.range.getSheet();
const shts = ['Sheet1','Sheet2','Sheet3'];
const idx = shts.indexOf(sh.getName())
if(~idx && e.range.columnStart == 2 && e.range.rowStart > 1 && e.value) {
const row_new = sh.getRange(e.range.rowStart,1,1,e.range.columnStart);
row_new.copyTo(sh.getRange(sh.getLastRow()+1,1,1,e.range.columnStart));
sh.deleteRow(e.range.rowStart);
}
}
In Google Sheets, range N16:N29 has content referenced as a query from another sheet. If I select any one of those cells N16:N29, I'd like to have its content displayed at cell N10. When the active cell is no longer one of N16:N29, N10 is blank.
I haven't seen any mention of this and am curious if it's possible.
function onSelectionChange(e) {
//e.source.toast('entry');
//Logger.log(JSON.stringify(e));
const sh = e.range.getSheet();
if(sh.getName() == 'Sheet1' && e.range.columnStart < 10 && e.range.rowStart < 17) {
sh.getRange("M1").setValue(e.range.getValue());
}
}
I imported data into Sheet1 A2:J16 and used the onSelectionChange() triggers to display data in M1 when you select a cell inside of the imported range.
You just have to edit the if statement to set e.range.columnStart > 15 && e.range.columnStart < 30 & e.range.rowStart == 14 and correct the sheet name and change M1 to N10
function onSelectionChange(e) {
//e.source.toast('entry');
//Logger.log(JSON.stringify(e));
const sh = e.range.getSheet();
if(sh.getName() == 'New Sheet Name' && e.range.columnStart > 15 && e.range.columnStart < 30 & e.range.rowStart == 14) {
sh.getRange("N10").setValue(e.range.getValue());
}
}
function onSelectionChange(e) {
//e.source.toast('entry');
//Logger.log(JSON.stringify(e));
const sh = e.range.getSheet();
if(sh.getName() == 'Form' && e.range.columnStart < 15 && e.range.columnStart > 13 && e.range.rowStart > 15 && e.range.rowStart < 30) {
sh.getRange("N10").setValue(e.range.getValue());
}
}
Cooper, thanks for your help! I'm new to Apps Script. I've been having trouble getting your code to work on my sheet. None of the selections I made would show at the target cell. After some experimenting, I changed columnStart from >15 <30 to <15 >13 and rowStart from ==14 to >15 <30. That worked.
The only thing I can't figure out is when I select a cell outside the script range (N16:N29), the target cell (N10) still holds the previous selection. I'd like for the target cell to go blank when that happens. Is that possible?
I am using the following function to change the contents of column 1 and 2 into ALL CAPS upon edit. This works well.
I would like it modified to skip the function if the contents of that cell is a hyperlink. Column 1 or 2 may occasionally contain a link to a folder and do not want to modify the contents of that cell if there is a link associated with it.
Am I able to use the ISURL() function somehow?
Any advice appreciated.
onEdit(e) {AllCaps(e);}
function AllCaps(e) {
var sheet, sheetName, colToCapitalize1, colToCapitalize2;
sheet = e.source.getActiveSheet();
colToCapitalize1 = 1;
colToCapitalize2 = 2;
sheetName = 'Sheet1';
if (sheet.getName() !== sheetName || e.range.rowStart < 1 || e.range.columnStart > 2 || typeof e.value == 'object') return;
e.range.setValue(e.value.toUpperCase())
}
I think that in your case, in order to check the hyperlink of the edited cell, I would like to propose the following modification. Please modify your script as follows.
From:
if (sheet.getName() !== sheetName || e.range.rowStart < 1 || e.range.columnStart > 2 || typeof e.value == 'object') return;
To
if (sheet.getName() !== sheetName || e.range.rowStart < 1 || e.range.columnStart > 2 || typeof e.value == 'object' || e.range.getRichTextValue().getRuns().filter(e => e.getLinkUrl()).length > 0) return;
In this modification, e.range.getRichTextValue().getRuns().filter(e => e.getLinkUrl()).length > 0 is added to the if statement. In this case, when the hyperlinks are included in the edited cell, it returns true. By this, e.range.setValue(e.value.toUpperCase()) is not run.
References:
getRichTextValue()
Related thread
How to extract the link from a cell now that links are not reflected as HYPERLINK?