简体   繁体   English

在Google Apps脚本中锁定服务和脚本属性

[英]Lock Service and Script Properties in Google Apps Script

I am writing a code on Google Apps Script. 我正在Google Apps脚本上编写代码。 I need to give an unique number for each user. 我需要给每个用户一个唯一的号码。

At first, I wrote some code with tryLock and ScriptProperties class. 首先,我使用tryLock和ScriptProperties类编写了一些代码。 It gave the same number for several persons when 6 users called the function at almost the same time. 当6个用户几乎同时调用该功能时,它为几个人提供了相同的号码。 So, now I am using waitLock and ScriptProperties. 因此,现在我正在使用waitLock和ScriptProperties。

Is there any difference between tryLock and waitLock in terms of locking ability? tryLock和waitLock在锁定能力方面有什么区别吗? Also, I am wondering the update timing of ScriptProperties. 另外,我想知道ScriptProperties的更新时间。 Is it updated immediately for all users? 是否为所有用户立即更新?

If you give advises on this issue, I really appreciate it. 如果您对此问题提供建议,我真的很感激。


//My code with tryLock: This gave the same number for 3 users in a test by 6 users. //我的带有tryLock的代码:在6个用户的测试中,这为3个用户提供了相同的数字。

var glock = LockService.getPublicLock();
if( glock.tryLock(10000) )
{
  var val = ScriptProperties.getProperty(proName) * 1 + 1;
  ScriptProperties.setProperty(proName, val);
  glock.releaseLock();
  return val;
} else { return null; }

//Another code with waitLock: This gave an unique number for each in a test by 8 users. //另一个带有waitLock的代码:在8位用户的测试中,每个代码都给出了唯一的编号。

var val = null;
try{
  var glock = LockService.getPublicLock();
  glock.waitLock(10000);
  val = ScriptProperties.getProperty(proName) * 1 + 1;
  ScriptProperties.setProperty(proName, val);
  glock.releaseLock();
} catch (e) { }
return val;

If you don't need the IDs to be sequential you might be better off using the timestamp instead: 如果您不需要ID是连续的,则最好使用时间戳代替:

var glock = LockService.getPublicLock();
if (glock.tryLock(10000)) {
  var val = (new Date()).getTime();
  glock.releaseLock();
  return val;
} else { 
  return null; 
}

The methods tryLock() and waitLock() work the same, it's just that the first returns false if it can't obtain a lock while the second throws an exception in that case. 方法tryLock()waitLock()工作原理相同,只是如果第一个方法无法获取锁定,则第一个方法返回false,而第二种方法则抛出异常。

If you want to identify unique users of a script, you can use a property in the userproperties store, since each user has a unique store. 如果要标识脚本的唯一用户,则可以使用userproperties存储中的属性,因为每个用户都有一个唯一存储。 If you've never seen him before then generate a unique number using a time stamp or something in his user property store. 如果您从未见过他,请使用时间戳记或他的用户属性存储库中的内容生成一个唯一的号码。 If you have seen him before his store will already contain the unique number you generated on his last visit. 如果您在他的商店之前见过他,那么他的商店将已经包含您上次访问时生成的唯一编号。 If you need to consolidate all numbers, copy them to the script property store. 如果需要合并所有数字,请将其复制到脚本属性存储中。

For example, see http://ramblings.mcpher.com/Home/excelquirks/gassnips/anonymoususerregistration 例如,请参见http://ramblings.mcpher.com/Home/excelquirks/gassnips/anonymoususerregistration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM