Frage

I have a Google Sites site with a List page.

I want to get the data from the ListItems into a database (ScriptDB or JDBC to MySQL/SQL Server.)

I'd like to fire an update as a result of an onUpdate type event, but can't find anything like that. Is the solution to run a script every so often?

The downside of running a script periodically is that I can't capture all changes, only the current state when the script runs. I would like a full audit of changes if possible. I'd also have to interrogate each ListItem to check the lastUpdated date to see if it is newer than the records already in the database, which seems like a lot of redundant processing.

Any advice?

War es hilfreich?

Lösung

Indeed you cannot run an onUpdate event as they are reserved for Spreadsheets.

However you have the information on the listItems and you can run a loop like this to get the update time and publication time of your items:

function getChanges(){
    var listItems=page.getListItems();
    var update;
    var datePublished;
    for(var i=0;i<listItems.length;++i){
        update=item[i].getLastUpdate();
        datePublished=item[i].getDatePublished();

        list of stuff to do like compare with the content of your database;
    }
}

A clock trigger is then the best solution to capture the changes (like a 10 minute trigger).

Unfortunately you cannot do more than this.

Cheers

Nicolas

Andere Tipps

Thanks Nicolas. In the end I already implemented something pretty similar:

function updatePeople(){
  for(var j in peopleList){
    // Check if the record in the peopleList is newer than the last database (not record) update.
    var personUpdated=peopleList[j].getLastUpdated().getTime();
    var dbUpdated= db.query({Type:"dbUpdated"}).next().dbUpdated;
    // If the record is newer than the last database update...
    if(personUpdated>dbUpdated)
    { 
      // ...check if the record exists in the database (check for current record using Is_Current = 1)
      // If it does, set the Valid_To date on the old record to be 1 second before the Valid_From date of the new record.
      var result = db.query({Type:"Person", Initials: peopleList[j].getValueByName("Initials"), Is_Current:1}).getSize();
      if(result>0){
        var oldPerson = db.query({Type:"Person", Initials: peopleList[j].getValueByName("Initials"), Is_Current:1}).next();
        var validTo = personUpdated-1000;
        oldPerson.Valid_To = validTo;
        oldPerson.Is_Current = 0;
        db.save(oldPerson); 
        // now add a new Person record.
        addPerson(j);
      }
      else {
        addPerson(j); 
      }
    }
  }
  updateDbUpdatedDate();
}

The addPerson() function is a pretty simple function to add a new record.

function addPerson(j){
  var person={};
  person["Valid_From"]=peopleList[j].getLastUpdated().getTime();
  person["Valid_To"]=9999999999999;
  person["Is_Current"]=1;
  person["Type"]="Person";
  person["Initials"]= peopleList[j].getValueByName("Initials");
  person["Name"]=peopleList[j].getValueByName("Name");
  person["Email_Address"]= peopleList[j].getValueByName("Email Address");
  person["Team"]=peopleList[j].getValueByName("Team");
  person["Grade"]=peopleList[j].getValueByName("Grade");
  person["Admin"]=peopleList[j].getValueByName("Admin");
  db.save(person); 
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top