Conditional Formatting for items created in the last 5 minutes in SharePoint 2010 - sharepoint

All,
I've been looking all day and have tried numerous solutions, but just can't get it to work. Our team projects a list that is constantly updated and we want to highlight only newly created items for 5 minutes. After 5 minutes, the row would return to normal. (FYI- the list is projected on a display and updated using AJAX asynchronous update every 15 seconds)
Basically, I want to set conditional formatting on list items created in the last 5 minutes. If the item was created in the last 5 minutes, the row will be highlighted. After the 5 minutes are up, the row would return to normal.
I tried SharePoint Designer conditional formatting by creating a calculated column in Date/Time format called "Created + 5" and tried to set an expression where the formatting is applied (row is highlighted) when "Created + 5" is greater than or equal to current date. So after 5 minutes, the row will no longer be highlighted (because the current date/time will exceed the "Created + 5" value)
Here is the expression from the SPD Advanced Condition Builder:
ddwrt:DateTimeTick(ddwrt:GenDisplayName(string($thisNode/#Created_x0020__x002b__x0020_5_x))) >=
ddwrt:DateTimeTick(ddwrt:GenDisplayName(string($Today)))
I think the problem is that the [Current Date] option ($Today in the expression builder) only accounts for date and not time. It looks like it just ends up highlighting everything that was created today, which is not very useful.
Any thoughts or help!? I have never messed with the advanced conditions because usually the basic stuff works fine for me! If anyone has any other ideas too like JavaScript or anything else that would work, I am open to that too as long as it will continuously update!
Thanks all!!!!

[Today] actually doesn't work properly in 2010, there are some workarounds though, e.g. https://abstractspaces.wordpress.com/2008/05/19/use-today-and-me-in-calculated-column/.
You can also use the approach with calculated column: https://blog.splibrarian.com/2012/06/06/using-calculated-columns-to-add-color-coding-to-your-sharepoint-lists/

Since you want this to update automatically without requiring someone to manually refresh the page, JavaScript is your best bet. You can have a function run repeatedly on a specified interval, checking the current date against the values in a date column.
Something like the following code would work, though you may need to tweak the CSS selectors specified in the calls to document.querySelector and querySelectorAll to match your particular HTML.
<script>
formatCell();
function formatCell(){
var frequencyToCheck = 2 /* num seconds between updates */
var minutes = 5; /* num minutes back to highlight */
var targetColumn = "Display name of the column you want to check";
var formatting = "background-color:darkred;color:red;font-weight:bold;";
var comparisonDate = new Date();
comparisonDate.setHours(comparisonDate.getHours() - minutes);
var tables = document.querySelectorAll("table.ms-listviewtable"); /* should get all list view web parts on the page */
var t_i = 0;
while(t_i < tables.length){
var headings = tables[t_i].rows[0].cells;
var columnIndex = null;
var h_i = 0;
while(h_i < headings.length){
var heading = headings[h_i].querySelector("div:first-child");
if(heading != null){
var displayName = heading.DisplayName ? heading.DisplayName : (heading.innerText ? heading.innerText : heading.textContent);
displayName = displayName.replace(/^\s+|\s+$/g,''); /* removes leading and trailing whitespace */
if(displayName === targetColumn){
columnIndex = h_i;
break;
}
}
h_i += 1;
}
if(columnIndex != null){ /* we found a matching heading */
var rows = tables[t_i].rows;
for(var i = (rows.length > 0 ? 1 : 0); i < rows.length; i++){
var cells = rows[i].children;
if(cells.length <= columnIndex){continue;}
var valueToEval = cells[columnIndex].innerText ? cells[columnIndex].innerText : cells[columnIndex].textContent;
if(typeof valueToEval == "undefined"){valueToEval = "";}
valueToEval = new Date(valueToEval);
if(valueToEval > comparisonDate){
cells[columnIndex].setAttribute("style",formatting);
}else{
cells[columnIndex].setAttribute("style","");
}
}
}
t_i +=1;
}
setTimeout(formatCell,frequencyToCheck * 1000);
}
</script>
One potential pitfall is that while this approach will "age" records appropriately based on their displayed values (causing them to stop being highlighted as they grow stale), it won't automatically pick up new changes to the list; you'd need to refresh the page (or at least refresh the view in the web part) whenever you want to see updated information.

Related

Highlight dates closer than 3 months away

I have a simple spreadsheet listing certificates and expiry dates. Before we moved this to sharepoint online we had a macro than on opening the spreadsheet would check dates in a range of cells and highlight those within three months. It was intended to highlight anything up for renewal before it expired.
I appreciate macros are not an option in Excel online. Can this (or something very similar) be achieved in Office Scripting?
It should be possible to create an Office Script that highlights cells with a date that is within three months of the present.
Something Like:
function main(workbook: ExcelScript.Workbook)
{
// highlight all days between today and the next number of days out
// Number of days to look ahead
const daysOut = 90;
// Excel by default stores dates as the number of days after January 1, 1900
const dayMin = currentDaysSince1900();
const dayMax = dayMin + daysOut;
// Need to target the column to look at and how far down the column
const columnToLookAt = "A";
const rowStart = 1;
const rowEnd = 4;
const rangeAddress = `${columnToLookAt}${rowStart}:${columnToLookAt}${rowEnd}`;
const sheet = workbook.getActiveWorksheet();
// get range column
const range = sheet.getRange("A1:A3");
const values = range.getValues();
// iterate through the rows of values
for (let i =0 ; i < values.length; i++) {
const value = values[i][0];
console.log(value);
if (typeof value === "number") {
// only look at numbers
if (value >= dayMin && value <=dayMax ) {
// highlight
const rangeAddress = `${columnToLookAt}${rowStart +i}`;
const range = sheet.getRange(rangeAddress);
range.getFormat().getFill().setColor("yellow");
}
}
}
}
/**
* Current Days since Jan 1 1900
* Equivalent to number of current excel day
*/
function currentDaysSince1900() {
// method returns the number of milliseconds elapsed since January 1, 1970
const nowMilliseconds = Date.now();
const millisecondsPerDay = 24 * 60 * 60 * 1000 ;
const nowDays = Math.floor(nowMilliseconds / millisecondsPerDay);
const daysBetween1900And1970 = 25567;
const elapsed = nowDays + daysBetween1900And1970 +2; // add two to include both jan 1s
return elapsed;
}
In terms of triggering the script:
Office Script does not currently support running a script on opening a workbook.
You can manually trigger the script whenever you like.
You can also create a Microsoft Power Automate flow to run the script every day to keep the workbook updated.
More Office Script resources:
Official Microsoft Office Script Date Example
Official Microsoft Office Script Examples
wandyezj office script examples

How can I search for edited rows on Sheets to compare the values and change the timestamp on different columns accordingly?

I have been looking endlessly for a method to compare values on different columns in the same row, to know which cell I should update.
The speradsheet is a simple model of stock management (it's quite simple and I've been doing it manually), but I wanted a 'faster'(*) ou automated way of updating the amounts os each item, and the timestamps (which are two: one for adding units to the stock, and one for withdrawing).
The obstacles so far are:
The onEdit() function won't work on automated changes like macros, so it's off the table;
I need to scan the whole spreadsheet to find every filled cell on column D, which carries the value i'm adding to or subtracting from my column C;
-For this i have already setup do filter the column 'from Z to A' to get all the cells with values on them, but the amount of items changed can vary, so i cant set a search with a fixed number of rows.
Since my sheet has over 90 entries (likely to increase) of at least 6 columns each, a for loop with if statements takes too long, (*)but execution time is not exactly the main concern right now.
The code is as follows, and I'll be attaching a picture of the sheet I'm working with.
/** #OnlyCurrentDoc */
function geral() {
filtro();
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('G2').activate();
spreadsheet.getCurrentCell().setFormula('=C2+D2');
spreadsheet.getActiveRange().autoFill(spreadsheet.getRange('G2:G92'), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
var currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
currentCell.activateAsCurrentCell();
spreadsheet.getRange('C2').activate();
spreadsheet.getRange('G2:G').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
spreadsheet.getRange('G:G').activate();
spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
//adds the input OR output timestamp depending on the value in D column
//!!WORK IN PROGRESS!! --> here's where it gets tricky, and that's what I got so far (which doesn't work)
/*
for (var i = 2; i < 100; i++) {
spreadsheet.getRange('J2').setValue("TESTE");
var cell1 = spreadsheet.getRange('????').getValue(); //from this point on, I don't know how to proceed
var cell2 = spreadsheet.getRange('????').getValue();
spreadsheet.getRange('J2').setValue("TESTE2");
if(cell1 > cell2){
spreadsheet.getRange('????').activate();
spreadsheet.getActiveCell().setValue(new Date());
}
else if(cell1 < cell2){
spreadsheet.getRange('????').activate();
spreadsheet.getActiveCell().setValue(new Date());
}
}
*/
spreadsheet.getRange('D2:D').activate();
spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
};
function filtro() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('D:D').activate();
spreadsheet.getActiveSheet().sort(4, false);
};
EDIT: With my review after #IrvingJayG.'s comment, I noticed a few mistakes and unnecessary extra steps, so instead of doing all the copy-paste-delete dance and then compare results, I'd go for the pseudocode below:
//Ci's value pre-exists in the sheet, where i is the row index
//manually input Di value
//set formula for Gi = Ci+Di
//and then compare either Ci and Gi, or Di and 0
if(Di > 0){
//the following steps can be defined as a new function for each case, (e.g. updateIn() and updateOut())
copy Gi to Ci;
update Ei with new Date();
delete Gi and Di;
}
else if(Di < 0){
copy Gi to Ci;
update Fi with new Date();
delete Gi and Di;
}
Unfortunately, it still doesn't solve my problem, just simplifies the code by a lot.
Sheet example
RECOMMENDATION:
I've created a sample sheet (based on your attached example sheet) with 6 rows of data and with 4 random sample cell values on Column D. Then, I've created a sample script below, where you can use a reference:
NOTE: This script will scan every row on your sheet that has data (e.g. if you have 30 rows of data, it will scan every row one-by-one until it reaches the 30th row) and may slow-down once you have bunch of data on it. That's the catch because it's an expected behavior
SAMPLE SHEET:
SCRIPT:
function onOpen() { //[OPTIONAL] Created a custom menu "Timestamp" on your Spreadsheet, where you can run the script
var ui = SpreadsheetApp.getUi();
ui.createMenu('Timestamp')
.addItem('Automate Timestamp', 'mainFunction')
.addToUi();
}
function mainFunction() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('D:D').activate();
spreadsheet.getActiveSheet().sort(4, false);
automateSheetCheck();
}
function automateSheetCheck(){
var d = new Date();
var formattedDate = Utilities.formatDate(d, "GMT", "MM/dd/yyyy HH:mm:ss");
var spreadsheet = SpreadsheetApp.getActive();
var currentRow = spreadsheet.getDataRange().getLastRow(); //Get the last row with value on your sheet data as a whole to only scan rows with values
for(var x =2; x<=currentRow; x++){ //Loop starts at row 2
if(spreadsheet.getRange("D"+x).getValue() == ""){ //Checks if D (row# or x) value is null
Logger.log("Cell D"+x+" is empty"); //Logs the result for review
}else{
var res = spreadsheet.getRange("C"+x).getValue() + spreadsheet.getRange("D"+x).getValue(); //SUM of C & D values
if(spreadsheet.getRange("D"+x).getValue() > 0){ // If D value is greater than 0, E cell is updated with new timestamp and then C value is replaced with res
Logger.log("Updated Timestamp on cell E"+x + " because D"+x+ " with value of "+ spreadsheet.getRange("D"+x).getValue() +" is greater than 0"); //Logs the result for review
spreadsheet.getRange("E"+x).setValue(formattedDate);
spreadsheet.getRange("C"+x).setValue(res); //Replace C value with "res"
spreadsheet.getRange("D"+x).setValue(""); //remove D value
}else{ // If D value is less than 0, F cell is updated with a new timestamp
Logger.log("Updated Timestamp on cell F"+x + " because D"+x+ " with value of "+ spreadsheet.getRange("D"+x).getValue() +" is less than 0"); //Logs the result for review
spreadsheet.getRange("F"+x).setValue(formattedDate);
spreadsheet.getRange("C"+x).setValue(res); //Replace C value with "res"
spreadsheet.getRange("D"+x).setValue(""); //remove D value
}
}
}
}
RESULT:
After running the script, the will be the result on the sample sheet:
Here's the Execution Logs, where that you can review what happened after running the code:

Is there a google-script method like Application.Intersect(Target, Range) in Excel?

Now I understand that the question is deeper, and is connected with the tracking of events.
In Excel, I use this code:
If Not Intersect(Target, Sh.Range("$A$1:$A$300")) Is Nothing sub_do_something()
Here, Target - the address of the selected cell, Intersect determines whether the cell belongs to the specified range.
I use it in the system for filling and calculating the costing of the project.
The user clicks a row in a specific section of the calculation template. The script determines the address of the selected cell and switches the user to a specific sheet of the directory. Next, the user clicks on the desired line of the directory, the script copies a certain range of cells in the line and returns the user back to the calculation. When this happens, the copied data is inserted into a range of cells, starting with the selected one.
Thus, the creating a calculation, in which there can be more than 100 positions, is greatly simplified.
In Excel, everything works fine, but soon I plan to transfer this project to a cloud-based service, and Google Sheets is the best option.
Alas, only some events can be tracked in GAS, for example, using onOpen or onEdit triggers.
Excel has much more tracked events.
After a search on the StackOverflow, I found several similar issues related to tracing events, for example, How to find where user's cursor is in Document-bound script, Can we implement some code that fires upon selecting something in google document?, Google app script monitor spreadsheet selected ranges.
From the answers to these questions, it is clear that in GAS there is no such simple solution as Intersect(Target, Range) in Excel.
The last example uses the side menu, running a script from it that queries the sheet 5 times per second, and displays the address of the active cell in the "data" field.
Unfortunately, this code does not work for me. In the debugger, the getActiveRange() function works fine, but this code does not work:
$(document).ready(() => {
setInterval(()=>{
google.script.run.withSuccessHandler(log).getActiveRange();
},200)
})
log(e) => {
$('#data').val(e)
}
Question.
If someone did something similar, please share your experience.
Or tell me why this example does not work. If he can be reanimated, I will adapt him to solve my task.
I worked on a similar project and here's the solution:
function onSelectionChange(e)
{
var ss = e.source;
var Sh = ss.getActiveSheet();
var range = Sh.getRange("A1:A300");
var target = e.source.getActiveRange();
//check for intersection
if(RangeIntersects(target, range))
{
Logger.log("Changed Row: " + target.getRow() + "\nValue: " + target.getValue());
}
}
//returns true if target intersects with the predefined range
function RangeIntersects(target, range)
{
return (target.getLastRow() >= range.getRow()) && (range.getLastRow() >= target.getRow()) && (target.getLastColumn() >= range.getColumn()) && (range.getLastColumn() >= target.getColumn());
}
Here's an idea. I can't quite get it to work though.
Maybe someone else can give a better answer.
Also, having functions running 24/7 is not possible with GAS, I think, as there are limits to the total run-time. You may wish to add a code-guard that exits the script if the last update time is longer than 10 minutes ago or something.
function checkSelection() {
var spreadsheet = SpreadsheetApp.getActive();
var targetRange = spreadsheet.getRange('activate');
// Change your named ranged name here
var tCol = targetRange.getColumn();
var tLastCol = targetRange.getLastColumn();
var tRow = targetRange.getRow();
var tLastRow = targetRange.getLastRow();
var num = 0;
for (num; num < 115; ++num) {
// Repeats the code below 100 times
var range = spreadsheet.getActiveRange();
var row = range.getRow();
var col = range.getColumn();
if (col >= tCol && col <= tLastCol && row >= tRow && row <= tLastRow) {
range.setBackground('#000000');
// Change the code in this block to your code.
}
SpreadsheetApp.flush();
Utilities.sleep(500);
// Waits half a second before repeating
}
}
115 repetitions * 500ms wait seems to run for almost a minute, then the trigger will fire the whole function again.
Intersection of two Ranges
You can use this to calculate intersection of two ranges. It requires an object in the form of: {rg1:'A1Notation String',rg2:'A1Notation String'}
function calculateIntersection1(rgObj) {
var iObj={};
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var rg1=sh.getRange(rgObj.rg1);
var rg2=sh.getRange(rgObj.rg2);
var iObj={rg1colst:rg1.getColumn(),rg1colen:rg1.getColumn()+rg1.getWidth()-1,rg1rowst:rg1.getRow(),rg1rowen:rg1.getRow()+rg1.getHeight()-1,rg2colst:rg2.getColumn(),rg2colen:rg2.getColumn()+rg2.getWidth()-1,rg2rowst:rg2.getRow(),rg2rowen:rg2.getRow()+rg2.getHeight()-1};
if(iObj.rg1colst>iObj.rg2colen || iObj.rg1colen<iObj.rg2colst || iObj.rg1rowst>iObj.rg2rowen || iObj.rg1rowen<iObj.rg2rowst || iObj.rg2colst>iObj.rg1colen || iObj.rg2colen<iObj.rg1colst || iObj.rg2rowst>iObj.rg1rowen || iObj.rg2rowen<iObj.rg1rowst) {
return '<h1>No intersecting cells</h1>';
}else{
var vA1=rg1.getValues();
var v1=[];
var vA2=rg2.getValues();
var v2=[];
for(var i=0;i<vA1.length;i++){
for(var j=0;j<vA1[i].length;j++){
var s=Utilities.formatString('(%s,%s)', iObj.rg1rowst+i,iObj.rg1colst+j);
v1.push(s);
}
}
for(var i=0;i<vA2.length;i++){
for(var j=0;j<vA2[i].length;j++){
var s=Utilities.formatString('(%s,%s)', iObj.rg2rowst+i,iObj.rg2colst+j);
v2.push(s);
}
}
var oA=[];
for(var i=0;i<v1.length;i++){
var idx=v2.indexOf(v1[i]);
if(idx>-1){
oA.push(v2[idx]);
}
}
return Utilities.formatString('Intersecting Cells: %s', oA.join(', '));
}
}
It either returns the string "No Intersecting Cells" or a string identifying the intersecting cells in (row, column) format.

Delete rows after a date has passed automatically for Google Spreadsheets [duplicate]

I'd like to be able to delete an entire row in a Google Spreadsheets if the value entered for say column "C" in that row is 0 or blank. Is there a simple script I could write to accomplish this?
Thanks!
I can suggest a simple solution without using a script !!
Lets say you want to delete rows with empty text in column C.
Sort the data (Data Menu -> Sort sheet by column C, A->Z) in the sheet w.r.t column C, so all your empty text rows will be available together.
Just select those rows all together and right-click -> delete rows.
Then you can re-sort your data according to the column you need.
Done.
function onEdit(e) {
//Logger.log(JSON.stringify(e));
//{"source":{},"range":{"rowStart":1,"rowEnd":1,"columnEnd":1,"columnStart":1},"value":"1","user":{"email":"","nickname":""},"authMode":{}}
try {
var ss = e.source; // Just pull the spreadsheet object from the one already being passed to onEdit
var s = ss.getActiveSheet();
// Conditions are by sheet and a single cell in a certain column
if (s.getName() == 'Sheet1' && // change to your own
e.range.columnStart == 3 && e.range.columnEnd == 3 && // only look at edits happening in col C which is 3
e.range.rowStart == e.range.rowEnd ) { // only look at single row edits which will equal a single cell
checkCellValue(e);
}
} catch (error) { Logger.log(error); }
};
function checkCellValue(e) {
if ( !e.value || e.value == 0) { // Delete if value is zero or empty
e.source.getActiveSheet().deleteRow(e.range.rowStart);
}
}
This only looks at the value from a single cell edit now and not the values in the whole sheet.
I wrote this script to do the same thing for one of my Google spreadsheets. I wanted to be able to run the script after all the data was in the spreadsheet so I have the script adding a menu option to run the script.
/**
* Deletes rows in the active spreadsheet that contain 0 or
* a blank valuein column "C".
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 0;
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
if (row[2] == 0 || row[2] == '') {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the readRows() function specified above.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Remove rows where column C is 0 or blank",
functionName : "readRows"
}];
sheet.addMenu("Script Center Menu", entries);
};
Test spreadsheet before:
Running script from menu:
After running script:
I was having a few problems with scripts so my workaround was to use the "Filter" tool.
Select all spreadsheet data
Click filter tool icon (looks like wine glass)
Click the newly available filter icon in the first cell of the column you wish to search.
Select "Filter By Condition" > Set the conditions (I was using "Text Contains" > "word")
This will leave the rows that contain the word your searching for and they can be deleted by bulk selecting them while holding the shift key > right click > delete rows.
This is what I managed to make work. You can see that I looped backwards through the sheet so that as a row was deleted the next row wouldn't be skipped. I hope this helps somebody.
function UpdateLog() {
var returnSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('RetLog');
var rowCount = returnSheet.getLastRow();
for (i = rowCount; i > 0; i--) {
var rrCell = 'G' + i;
var cell = returnSheet.getRange(rrCell).getValue();
if (cell > 0 ){
logSheet.
returnSheet.deleteRow(i);
}
}
}
quite simple request. Try this :
function try_It(){
deleteRow(2); //// choose col = 2 for column C
}
function deleteRow(col){ // col is the index of the column to check for 0 or empty
var sh = SpreadsheetApp.getActiveSheet();
var data = sh.getDataRange().getValues();
var targetData = new Array();
for(n=0;n<data.length;++n){
if(data[n][col]!='' && data[n][col]!=0){ targetData.push(data[n])};
}
Logger.log(targetData);
sh.getDataRange().clear();
sh.getRange(1,1,targetData.length,targetData[0].length).setValues(targetData);
}
EDIT : re-reading the question I'm not sure if the question is asking for a 'live' on Edit function or a function (like this above) to apply after data has been entered... It's not very clear to me... so feel free to be more accurate if necessary ;)
There is a simpler way:
Use filtering to only show the rows which you want to delete. For example, my column based on which I want to delete rows had categories on them, A, B, C. Through the filtering interface I selected only A and B, which I wanted to delete.
Select all rows and delete them. Doing this, in my example, effectively selected all A and B rows and deleted them; now my spreadsheet does not show any rows.
Turn off the filter. This unhides my C rows. Done!
There is a short way to solve that instead of a script.
Select entire data > Go to menu > click Data tab > select create filter > click on filter next to column header > pop-up will appear then check values you want to delete > click okay and copy the filtered data to a different sheet > FINISH
reading your question carefully, I came up with this solution:
function onOpen() {
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// create menu
var menu = [{name: "Evaluate Column C", functionName: "deleteRow"}];
// add to menu
ss.addMenu("Check", menu);
}
function deleteRow() {
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get active/selected row
var activeRow = ss.getActiveRange().getRowIndex();
// get content column C
var columnC = ss.getRange("C"+activeRow).getValue();
// evaluate whether content is blank or 0 (null)
if (columnC == '' || columnC == 0) {
ss.deleteRow(parseInt(activeRow));
}
}
This script will create a menu upon file load and will enable you to delete a row, based on those criteria set in column C, or not.
This simple code did the job for me!
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); // get active spreadsheet
var activeRow = ss.getActiveRange().getRowIndex(); // get active/selected row
var start=1;
var end=650;
var match='';
var match2=0; //Edit this according to your choice.
for (var i = start; i <= end; i++) {
var columnC = ss.getRange("C"+i).getValue();
if (columnC ==match || columnC ==match2){ ss.deleteRow(i); }
}
}
The below code was able to delete rows containing a date more than 50 days before today in a particular column G , move these row values to back up sheet and delete the rows from source sheet.
The code is better as it deletes the rows at one go rather than deleting one by one. Runs much faster.
It does not copy back values like some solutions suggested (by pushing into an array and copying back to sheet). If I follow that logic, I am losing formulas contained in these cells.
I run the function everyday in the night (scheduled) when no one is using the sheet.
function delete_old(){
//delete > 50 day old records and copy to backup
//run daily from owner login
var ss = SpreadsheetApp.getActiveSpreadsheet();
var bill = ss.getSheetByName("Allotted");
var backss = SpreadsheetApp.openById("..."); //backup spreadsheet
var bill2 = backss.getSheetByName("Allotted");
var today=new Date();
//process allotted sheet (bills)
bill.getRange(1, 1, bill.getMaxRows(), bill.getMaxColumns()).activate();
ss.getActiveRange().offset(1, 0, ss.getActiveRange().getNumRows() - 1).sort({column: 7, ascending: true});
var data = bill.getDataRange().getValues();
var delData = new Array();
for(n=data.length-1; n>1; n--){
if(data[n][6] !=="" && data[n][6] < today.getTime()-(50*24*3600*1000) ){ //change the condition as per your situation
delData.push(data[n]);
}//if
}//for
//get first and last row no to be deleted
for(n=1;n<data.length; n++){
if(data[n][6] !=="" && data[n][6] < today.getTime()-(50*24*3600*1000) ){
var strow=n+1 ; //first row
break
}//if
}//for
for(n=data.length-1; n>1; n--){
if(data[n][6] !=="" && data[n][6] < today.getTime()-(50*24*3600*1000) ){
var ltrow=n+1 ; //last row
break
}//if
}//for
var bill2lr=bill2.getLastRow();
bill2.getRange((bill2lr+1),1,delData.length,delData[0].length).setValues(delData);
bill.deleteRows(strow, 1+ltrow-strow);
bill.getRange(1, 1, bill.getMaxRows(), bill.getMaxColumns()).activate();
ss.getActiveRange().offset(1, 0, ss.getActiveRange().getNumRows() - 1).sort({column: 6, ascending: true}); //get back ordinal sorting order as per column F
}//function

Getting total View entries including categories - performance issue

I'm using a custom footer on my View Control; there I have for example:
Displaying 1 - 25 of 34200
My underlying View is Categorized so the total of entries should include categories as well.
So far the only way I'm able to find the total of entries including categories is using NotesViewNavigator; however, performance is not acceptable since it's taking around 27 seconds to compute this piece of code.
I'm sure the issue is with line var nav:NotesViewNavigator = view1.createViewNav(); because I added some debugger info:
start = new Date().getTime();
var viewPanel1:com.ibm.xsp.component.xp.XspViewPanel = getComponent("dataView1");
var nav:NotesViewNavigator = view1.createViewNav();
if (viewScope.VendorSrch == "" || viewScope.VendorSrch == null){
var total = nav.getCount();
}else{
var total = viewPanel1.getRowCount();// View can be filtered by user as well (using categoryFilter property)
}
var from =(viewPanel1.getFirst() < total? (viewPanel1.getFirst() + 1 ) : total);
var tmpTo = viewPanel1.getFirst() + viewPanel1.getRows();
var to = (tmpTo < total? tmpTo : total);
var elapsed = new Date().getTime() - start;
print(elapsed + " ms");
"</b>Displaying <b>"+ from +"</b> - <b>"+ to + "</b> of " + "<b>"+total+"</b>"
Does anyone know how can I improve this piece of code?
Please note documents in this View have Readers fields as well which may be impacting the
performance of this operation.
You are trapped in performance hell. Read access protection is only and only stored inside the document. So when you ask your view navigator to get the count its only option is to open all involved documents - hence the poor performance. Read protection and performance are natural enemies (just imagine: you have an office where every door is locked and to move around you have to check all your keys every time if you have one with the matching lock number).
The way out of reader field introduced performance hell is to read only the entries you actually need (as outlined). It could be a little tricky if a user has access to documents based on name, group-membership and role (that would make one read per access), but it is still very much feasible. In this case you would use a repeat control and a object data source or managed bean, so the multiple passes happen in the background.
Bonus trick: if you add a column with the formula 1 (just the number) and add it up, while categorizing it, then you can just jump from naventry to next sibling (that would be the next category) and add the numbers --> much less reads involved and NO documents opened.
To stress again: nav.count needs to open all documents and is a BAD idea, anything that requires read access to be checked is a bad idea, so using one (or more) viewNav based on access rights that actually only read documents the user can read is the way to go.
Let me know if you need more hints
I tried several approaches and using this loop reduced the time from 27 seconds average to 2.7 seconds:
start = new Date().getTime();
var viewPanel1:com.ibm.xsp.component.xp.XspViewPanel = getComponent("dataView1");
var nav:NotesViewNavigator = view1.createViewNav();
nav.setEntryOptions(NotesViewNavigator.VN_ENTRYOPT_NOCOLUMNVALUES);
// disable autoupdate
view1.setAutoUpdate(false);
if (viewScope.VendorSrch == "" || viewScope.VendorSrch == null){
nav.setBufferMaxEntries(400);
//var total = nav.getCount(); // This works but slow
// peform lookup
var vwentry:NotesViewEntry = nav.getFirst();
var vwentrytmp:NotesViewEntry = null;
count = 0;
while (vwentry != null){
count = count + 1;
// Get entry and go recycle
vwentrytmp = nav.getNext(vwentry);
vwentry.recycle();
vwentry = vwentrytmp;
}
total = count;
}else{
var total = viewPanel1.getRowCount();
}
var from =(viewPanel1.getFirst() < total? (viewPanel1.getFirst() + 1 ) : total);
var tmpTo = viewPanel1.getFirst() + viewPanel1.getRows();
var to = (tmpTo < total? tmpTo : total);
var elapsed = new Date().getTime() - start;
print(elapsed + " ms");
"</b>Displaying <b>"+ from +"</b> - <b>"+ to + "</b> of " + "<b>" + total + "</b>"

Resources