Google Sites ListItem数据到数据库事件

时间:2013-06-09 09:19:53

标签: database events google-apps-script listitem google-sites

我有一个带有列表页面的Google协作平台网站。

我想将ListItems中的数据导入数据库(ScriptDB或JDBC到MySQL / SQL Server。)

我想通过onUpdate类型事件触发更新,但找不到类似的东西。是经常运行脚本的解决方案吗?

定期运行脚本的缺点是我无法捕获所有更改,只能捕获脚本运行时的当前状态。如果可能的话,我希望对变更进行全面审核。我还必须询问每个ListItem以检查lastUpdated日期,看它是否比数据库中已有的记录更新,这似乎是很多冗余处理。

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

实际上,您无法运行onUpdate事件,因为它们是为Spreadsheets保留的。

但是你有关于listItems的信息,你可以运行这样的循环来获得你的项目的更新时间和发布时间:

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;
    }
}

时钟触发器是捕获更改的最佳解决方案(如10分钟触发器)。

不幸的是,你不能做到这一点。

干杯

尼古拉斯

答案 1 :(得分:0)

谢谢尼古拉斯。最后我已经实现了非常类似的东西:

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();
}

addPerson()函数是一个非常简单的函数来添加新记录。

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); 
}