您可以使用 ScriptDB 来存储信息onLoad()
,然后将其读回onEdit()
。每次进行编辑时,您都可以再次调用onLoad()
以再次刷新单元格值的数据库,或者只是替换数据库中的相关信息。
添加notonEdit()
以触发资源>所有触发器菜单中的 FromSpreadsheet>onEdit() 事件。
这是代码:
function onLoad() {
var db = ScriptDb.getMyDb()
//get array of the sheet range that has something in it
var sheet = SpreadsheetApp.getActiveSheet()
var lastrow = sheet.getLastRow()
var lastcolumn = sheet.getLastColumn()
var subsheet = sheet.getRange(1, 1, lastrow, lastcolumn)
var values = subsheet.getValues()
//write that array into the ScriptDB of the project
for (i=1; i<=lastrow; i++){
for (j=1; j<=lastcolumn; j++){
var object = {type: "onEditfudge", row: i, column:j, value:values[i-1][j-1]}
db.save(object)
Logger.log(object) //log it to check its correct..
}
}
}
function BeforeonEdit(){
db = ScriptDb.getMyDb()
var newrange = SpreadsheetApp.getActiveRange()
//get 'old value'
var dbentry = db.query({type: "onEditfudge", row:newrange.getRow(),column:newrange.getColumn()}).next()
var oldvalue = dbentry.value
//overwrite the 'old value' with the 'new value' for the next onEdit() event
dbentry.value = newrange.getValue()
db.save(dbentry)
//return the old value to do something with in the calling function
return oldvalue
}
function notonEdit(){
//show new and old value
Browser.msgBox("Old value is: " + BeforeonEdit() + ". New Value is: " + SpreadsheetApp.getActiveRange().getValue())
}