List Item manipulation in google sites. or: Don't fear the ( date ) reaper

262 Views Asked by At

I am using a google sites list page as a sort of project tracker.

I have the following headers.

Project|Description|Start Date|Due Date

I find that a lot of my users are not very good about cleaning up their trackers so I wanted to implement a reaper that would look to see if the due date is less than the current date.

How would one go about this? I will tell you how I did it.

1

There are 1 best solutions below

0
On BEST ANSWER

My method to attack this culminated in the following.

function dontFearTheRepear() {

var site = SitesApp.getSiteByUrl(YourSiteHere);
var page = SitesApp.getPageByUrl(YourPageHere);

//Columns
var columns = page.getColumns();
var columnList = [];

//Rows
var listItems = page.getListItems();
var listItem = 0
var listList = [];

// Get todays date
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
  dd='0'+dd
} 
if(mm<10) {
  mm='0'+mm
 } 
today = yyyy+'-'+mm+'-'+dd;
Logger.log(today)


//Get Column Names
for (var i in columns) {
  var cName =columns[i].getName();
  columnList.push(cName);
}
Logger.log(columnList);
Logger.log("The number of columns is " + columnList.length);
Logger.log("The number of rows is " + listItems.length);

   // Get index of Due Date   
var dueDateValue = columnList.indexOf("Due Date");
Logger.log("The index of due date is " + dueDateValue);


//Get Due date column info
var dueDate = page.getListItems({ start:0, max: 50 });
var indexLength = dueDate.length; 
var rowCounter = 1

for ( var j in listItems){
    var listItem = listItems[j];
    var value = listItem.getValueByIndex(dueDateValue);
    Logger.log("Due date of row " + rowCounter + " Is " + value);
    //don't fear the reaper
    if (value < today) {
        listItem.deleteListItem();
        Logger.log(listItem + " was deleted")
    }
rowCounter++;
j++;
}
}

Seems to be working well and as long as there is a column header "Due Date" It seems I should be able to add or remove columns as needed.

I hope this helps some others as well.