keep Hyperlink when copying row using script - hyperlink

Certain cell in rows have hyperlink. need hyperlink to follow row when copied from sheet to sheet based on other cell value
function moveToTab() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var range = ss.getActiveRange();
var col = range.getColumn();
Logger.log(col);
var aSheet = ss.getActiveSheet();
var header = aSheet.getRange(10,col,1,1).getValue();
Logger.log(header);
if (header.toLowerCase() !== 'issued to') return;
var target = range.getValue();
if (target.toString().trim() === '') return;
if (target.toString().toLowerCase() === aSheet.getName().toLowerCase()) return;
var sheets = ss.getSheets();
var tSheet = sheets.filter(function(sheet) {
if (sheet.getName().toLowerCase() == target.toLowerCase()) return true;
else return false;
})[0];
if (!tSheet) return;
var row = range.getRow();
var values = aSheet.getRange(row, 1, 1, aSheet.getLastColumn()).getValues();
tSheet.appendRow(values[0]);
aSheet.deleteRow(row);
I know I am missing something simple. In my work book column "F" has a hyperlink to files. I am using the above script to move certain rows from sheet to sheet based on value in Column "K" and would like the hyper link in the cell in column "F" to follow teh row from sheet to sheet. What am i missing

Related

How can I assign a Google Sheet script to only one sheet?

I have this script here, but I want it to only run on ONE specific sheet named "Nes Smart Data" (SheetNo5). Currently it runs on all sheets and puts the data on cells where I don't want them to be. Can you help me correct the code? Thanks a lot!
function onEdit(e){
var sheet = SpreadsheetApp.getActiveSheet();
var range = e.range;
var column = range.getColumn();
var row = range.getRow();
var text;
if (column==11) { //Replace 2 with the column number of your comments cell//
var newRange = sheet.getRange(row,column-5); //If the date is in the next column
var today = Utilities.formatDate(new Date(),Session.getScriptTimeZone(),'dd.MM.yyyy');
newRange.setValue(today);
}
if (column == 11){
text = sheet.getRange(row, 23).getValue();
sheet.getRange(row, 23).setValue(text + e.value+".");
}
}
Replace the following variable and things should work as desired -
Current:
var sheet = SpreadsheetApp.getActiveSheet();
Modification proposed:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Nes Smart Data');
Let me know if it doesn't!

Deleting Row Based on Cell Values

I am attempting to create a additional menu option that allow me to automatically delete all rows that contain the text "Complete" in Column D. The script I have runs and finishes with no errors but does not delete the rows. I tried to find help on line and tweaked the code but same result
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 'Complete';
for (var i = 'Complete'; i <= numRows - 1; i++) {
var row = values[i];
if (row[2] == 'Complete' || row[2] == '') {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Delete Rows where column shows Complete Text",
functionName : "readRows"
}];
sheet.addMenu("Script Center Menu", entries);
};

How to make a cell in a row show date of most recent edit within that row

I'm working in Google Sheets on what is going to be a regularly accessed and edited document. What I would like is to have a column labelled "Date of Last Edit" and for each cell in that column to reflect the date of each cell row most recent edit. For example, if I edit info in cell C3 then I want the cell in column "Date of Last Edit" Row 3 to reflect the date I made the edit. I've found a few that are run by column but they only work for an individual column. I need it to cover all cells in a row that fall in front of "Date of Last Edit."
add this script to your sheet and change format and time zone if you need so ("GMT+1", "dd.MM.yyyy"):
function onEdit(event)
{
var sheet = event.source.getActiveSheet();
// note: actRng = the cell being updated
var actRng = event.source.getActiveRange();
var index = actRng.getRowIndex();
var cindex = actRng.getColumnIndex();
var dateCol = sheet.getLastColumn();
var lastCell = sheet.getRange(index,dateCol);
var date = Utilities.formatDate(new Date(), "GMT+1", "dd.MM.yyyy");
lastCell.setValue(date);
}
This code will update C:C with the email address of the user, and D:D with the current time of the same row if A:B are edited on the Data Entry tab. You'll need to set up an onChange() trigger to get it to work.
function userUpdate() {
/*
Changed the updated by/on to make it faster
Put in handling to make sure that if they delete a cell, that it only clears out the submitted by/on if ALL the values in A:B are blank
*/
// Main sheet details
var s = SpreadsheetApp.getActiveSheet();
var spreadsheetTimeZone = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
var lastUpdatedString = Utilities.formatDate(new Date(), spreadsheetTimeZone, "MM/dd/yyyy' 'HH:mm:ss");
var sessionEmail = Session.getActiveUser().getEmail();
// Check Sheet
if (s.getName() == "Data Entry") { //checks that we're on the correct sheet
var r = s.getActiveCell();
var row = r.getRowIndex();
var column = r.getColumn();
var activeRange = s.getRange("A" + row + ":B" + row);
//var activeRangeValues =
var activeRangeValuesLength = activeRange.getValues().toString().length;
if (row > 1) {
// Declare variables
var cellValue = r.getValue().toString();
// If the cellValue IS blank
if (activeRangeValuesLength < 5) {
sessionEmail = "";
lastUpdatedString = "";
}
// Update Submitted By / Submitted On
if (column < 5) {
var lastUpdatedBy = s.getRange("C" + row)
lastUpdatedBy.setValue(sessionEmail); // update column C
var lastUpdated = s.getRange("D" + row)
lastUpdated.setValue(lastUpdatedString); // update column D
}
}
}

Google Sheets script to ignore or include specific sheets

I am getting this error: TypeError: Cannot call method "getName" of null. I have used a script to replace the formulas of the active sheet with the cell values. However, I don't want to accidentally run this on a few sheets, so I want to be able to exclude or include only specific sheets. I have been following another post and came up with this, and now I have the error:
function freezeValues() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
//loop through all sheets and get name to see if it includes specific string
for (i = 0; i < sheets.length; i++) {
var sheet = ss.getSheetByName(sheets[i]);
var name = sheet.getName();
if (name.includes("String_00")) {
//get active sheet and replace range with values
var sheetActive = ss.getActiveSheet();
var range = sheetActive.getRange("A1:Z50");
range.copyTo(range, {contentsOnly: true});
} else {
continue;
//skip over all those that don't meet the condition
}
}
}
UPDATE:
Trying this:
function freezeValues() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
//loop through all sheets and get name to see if it includes specific string
for (i = 0; i < sheets.length; i++) {
//var sheet = ss.getSheetByName(sheets[i]);
var name = sheets[i].getName().toString();
if (name.indexOf("String_00") > -1) {
//get active sheet and replace range with values
//var sheetActive = ss.getActiveSheet();
var range = sheets.getRange("A1:Z50");
range.copyTo(range, {contentsOnly: true});
} else {
continue;
//skip over all those that don't meet the condition
}
}
}
But now it doesn't do anything. using .include was giving me an error, so I moved over to .indexOf() > -1. Does not freeze data as expected for any sheet, regardless of name.
I came up with 2 solutions for this, sort of started from the drawing board. One creates an array, a sort of blacklist, and then if the sheet name contains any item in the blacklist, then it returns and doesn't execute the replacement. The second solution searches for a pattern in the sheet name and only then continues on to the replacement. Both accomplish what I was trying to do. I think the code I found was a bit buggy and was a bit complicated for what I was doing. I didn't need to loop through all sheet names, just check the sheet name against given variables. Anyway, here they are:
This one creates the array blacklist of pages not to edit:
function freezeValues() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ['Sheet1', 'Sheet2', 'Sheet3', 'Sheet4'];
var sheetActive = ss.getActiveSheet();
if (sheets.indexOf(sheetActive.getName()) > -1) return;
var range = sheetActive.getRange("A1:Z50");
range.copyTo(range, {contentsOnly: true});
}
This one only searches for specific text in the sheet name before proceeding with the replacement.
function freezeValues() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetActive = ss.getActiveSheet();
var name = sheetActive.getName()
if (name.indexOf("String_00") > -1) {
var range = sheetActive.getRange("A1:Z50");
range.copyTo(range, {contentsOnly: true});
}
}
I'm sure there is a method to use sheet index value so if you want to skip the first 4 sheets, you can probably do that as well, so it doesn't call on names at all.
UPDATE
function freezeValues2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetActive = ss.getActiveSheet();
if (sheetActive.getIndex() > 4) {
var range = sheetActive.getRange("A1:Z50");
range.copyTo(range, {contentsOnly: true});
}
}

Create Random Invoice Generator

I have created a google sheet that contains a button that when clicked will create a random number in a particular column. However, I need the code to find the first empty cell in the column for the random number. The code I am currently using fills all the cells in the row verses one cell at a time when the button is clicked.
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [ {name:"Resubmit",functionName:"Resubmit"} ];
sheet.addMenu("Script", entries);
};
function Resubmit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var cell = sheet.getRange("A:A");
cell.setValue( Math.floor((Math.random()*1000000)+1) );
};
You can be more specific and give it a number of cells you want, as per docs:
getRange(row, column, numRows, numColumns)
You can append after the last row either by setting cell like this:
var cell = sheet.getRange(sheet.getLastRow() + 1, 1);
or by changing the function to this:
function Resubmit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");;
sheet.appendRow([Math.floor((Math.random()*1000000)+1)])
};

Resources