Want to create timestamp for time added and create unique incremental user ID. And also include a last changed column

后端 未结 1 1109
迷失自我
迷失自我 2021-01-27 03:14

I am using Google Sheets for a (originally planned to be) quick and easy database. Users submit a form, it is then sent to a Sheets for storing.

I have three columns tha

相关标签:
1条回答
  • Update from original poster here - I have a solution that isn't the most elegant, but serves my purposes. Criticism & suggestions very much welcome :)

    Now on to figuring out the best way to insert dynamic formulas via GAS...

    function onChange(e) {
    
    var userId = 1 // column A  
    var createdTime = 2 //column B
    var lastChangedTime = 24 //column X
    
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = e.source.getSheets()[1]; //gets the second sheet
    
    var Avals = ss.getRange("A2:A").getValues(); //gets all the values of column A 
    var Alast = Avals.filter(String).length; //puts all values in a string and gets the length
    
    var now = new Date();
    
    if (e.changeType == "INSERT_ROW") {
    
    //set the created time
    ss.getActiveSheet().getRange(ss.getActiveRange().getLastRow(), createdTime, 1, 1).setValue(new Date());
    //set the userID
    ss.getActiveSheet().getRange(ss.getActiveRange().getLastRow(), userId, 1, 1).setValue(Alast+1);
    
    //set lastChangedTime when the row is created 
    ss.getActiveSheet().getRange(ss.getActiveRange().getLastRow(), lastChangedTime, 1, 1).setValue(now);
       } 
    
      if (e.changeType == "EDIT") {
    
    //set date in same row as edit happens, at fixed column  
    ss.getActiveSheet().getRange(ss.getActiveRange().getLastRow(), lastChangedTime, 1, 1).setValue(new Date());
    
       }  
    }
    
    0 讨论(0)
提交回复
热议问题