如何在更新多列时在一列上设置时间戳

时间:2016-04-09 16:24:34

标签: google-sheets

我正在试图在goole表中设置时间戳。我有这个代码,但它只设置更新一列时的时间戳,我想设置多列更新时的时间戳。你能帮我解决这个问题。

这是代码。

$ node -v
v4.1.1
$ ./test
Now using node v0.10.40 (npm v2.14.8)
default -> 0.10.40 (-> v0.10.40)
NVM_PATH=/Users/william/.nvm/v0.10.40/lib/node
MANPATH=/Users/william/.nvm/v0.10.40/share/man:/Users/william/.rvm/rubies/ruby-2.1.2/share/man:/usr/local/share/man:/usr/share/man:/opt/X11/share/man:/usr/local/MacGPG2/share/man:/Users/william/.rvm/share/man:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/share/man:/Applications/Xcode.app/Contents/Developer/usr/share/man:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/share/man
PATH=/Users/william/.nvm/v0.10.40/bin:/Users/william/.rvm/gems/ruby-2.1.2/bin:/Users/william/.rvm/gems/ruby-2.1.2@global/bin:/Users/william/.rvm/rubies/ruby-2.1.2/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/MacGPG2/bin:~/local/bin:~/bin:/Users/william/.rvm/bin:/Users/william/.rvm/bin:./node_modules/.bin:/usr/local/heroku/bin
NVM_BIN=/Users/william/.nvm/v0.10.40/bin
$ node -v
v4.1.1
$ source ./test
Now using node v0.10.40 (npm v2.14.8)
default -> 0.10.40 (-> v0.10.40)
NVM_PATH=/Users/william/.nvm/v0.10.40/lib/node
MANPATH=/Users/william/.nvm/v0.10.40/share/man:/Users/william/.rvm/rubies/ruby-2.1.2/share/man:/usr/local/share/man:/usr/share/man:/opt/X11/share/man:/usr/local/MacGPG2/share/man:/Users/william/.rvm/share/man:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/share/man:/Applications/Xcode.app/Contents/Developer/usr/share/man:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/share/man
PATH=/Users/william/.nvm/v0.10.40/bin:/Users/william/.rvm/gems/ruby-2.1.2/bin:/Users/william/.rvm/gems/ruby-2.1.2@global/bin:/Users/william/.rvm/rubies/ruby-2.1.2/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/MacGPG2/bin:~/local/bin:~/bin:/Users/william/.rvm/bin:/Users/william/.rvm/bin:./node_modules/.bin:/usr/local/heroku/bin
NVM_BIN=/Users/william/.nvm/v0.10.40/bin
$ node -v
v0.10.40

1 个答案:

答案 0 :(得分:0)

一种可能的方法是使用Array for Column names:

  var updateColName = ["SINTHIA", "Lu", "La", "Li"]; 

并使用循环检查它们:

for (var i = 0; i < updateColName.length; i++) {
    // good code goes here   
}

并且不要忘记将updateColName替换为updateColName[i]

多数民众赞成!

&#13;
&#13;
function onEdit(event) 
{ 
  var timezone = "GMT-04:00"; 
  var timestamp_format = "MM/dd/yyyy hh:mm:ss"; // Timestamp 12hour Format . 
  var updateColName = ["SINTHIA", "Lu", "La", "Li"]; 
  
  var timeStampColName = "FECHA SOLICITADO";
  var sheet = event.source.getSheetByName('Sheet1'); //Name of the sheet where you want to run this script.

  for (var i = 0; i < updateColName.length; i++) {

  var actRng = event.source.getActiveRange(); 
  var editColumn = actRng.getColumn(); 
  var index = actRng.getRowIndex(); 
  var headers = sheet.getRange(2, 2, 2, sheet.getLastColumn()).getValues(); 
  var dateCol = headers[0].indexOf(timeStampColName); 
  var updateCol = headers[0].indexOf(updateColName[i]); updateCol = updateCol+2; 
  if (dateCol > -2 && index > 2 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself! 
   var cell = sheet.getRange(index, dateCol + 2);
   var date = Utilities.formatDate(new Date(), timezone, timestamp_format); 
   cell.setValue(date); 
  } 
  }
}
&#13;
&#13;
&#13;

查看Example file