如何在Google Apps脚本Webapp上为并发用户提供便利? [英] How can I facilitate concurrent users on a Google Apps Script Webapp?

查看:48
本文介绍了如何在Google Apps脚本Webapp上为并发用户提供便利?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在创建一个可读写Google表格的网络应用.本质上,它是一个表单应用程序,它从HTML表单获取输入,并使用 appendRow()(以及随后使用 getRange() setValue()将更多数据附加到该行).每行代表要填写的表单的一个实例.请参阅写入过程的示例代码.

 /*每当按下下一步"按钮时在Index.html中调用的函数.这从HTML表单获取所有输入并将其存储在电子表格中.参数:inputArray:一个1x3数组,其中包含比例的所有者,参考和类型.*/函数addProjectInputs(inputArray){var sheetName =输入";var电子表格= SpreadsheetApp.getActiveSpreadsheet();var sheet =电子表格.getSheetByName(sheetName);Logger.log(sheet);var d = new Date();var tz =电子表格.getSpreadsheetTimeZone();var d = [Utilities.formatDate(d,tz,"dd/MM/yyyy")];var data = d.concat(inputArray);Logger.log(数据);sheet.appendRow(data);}/*函数,将更多数据附加到从中创建的行上addProjectInputs()*/函数addFurtherInputs(idlerArray){var sheetName =输入";var电子表格= SpreadsheetApp.getActiveSpreadsheet();var sheet =电子表格.getSheetByName(sheetName);var lastRow = sheet.getLastRow();var lastColumn = sheet.getLastColumn()-42;var arrayEmpty = idlerArray.every(function(i){return i ===``;});Logger.log(arrayEmpty);如果(arrayEmpty!== true){对于(i = 0; i< idlerArray.length; i ++){var range3 = sheet.getRange(lastRow,lastColumn + 1 + i,1,1);range3.setValue(idlerArray [i]);}}} 

应用程序的摘要页面(第5页,在所有编写步骤之后出现),然后读取此数据(使用 .getRange().getValues()),然后将其显示在页面上./p>

当一个用户在上面时,它可以正常工作-但是此应用程序最多可能同时被20个人使用.这是一个问题.例如,如果第二个用户开始使用该应用程序,那么他们的数据将通过添加新行开始覆盖第一个用户的数据,从而存档"第一个用户的数据.我遇到了以下可能的解决方案:

  1. 使用LockService强制一次只有一个人使用该应用程序(这不是理想的选择,因为该应用程序将用于报价客户,而并不想让用户真正等待).
  2. 多线程
  3. 堆栈

我不确定2或3如何工作,甚至在GAS中也不可能.谁能为多个用户提供便利,如何解决?

预先感谢

解决方案

除了锁定之外,我还可以通过为每个用户会话生成一个唯一的ID来解决此问题,您可以将其存储在工作表中,并在以后用于匹配值

在您的入口点[可能是doGet(),也许是doPost()],使用 var session_id = Utilities.getUuid();

生成一个UUID

然后将该会话ID传递到您的模板,并将其包含在将来的所有请求中,返回给Apps脚本.

将UUID写到 addProjectInputs()函数的工作表中的一列,然后修改您的 addFurtherInputs()函数以根据UUID找到正确的行,(而不是总是获取最后一个),并将更新内容写入该行.

这种方法保证您可以让许多用户工作而不必担心彼此覆盖,并且不会因锁定而造成延迟.

请参见 https://developers.google.com/apps-script/reference/utilities/utilities#getUuid()

I'm currently creating a webapp that reads and writes to a Google Sheet. It is essentially a form app which takes inputs from a HTML form, writes it into a sheet using appendRow() (and subsequent use of getRange() and setValue() to append further data onto that row). Each row represents an instance of the form being filled. See example code of the write process.

/*
Function that is called in Index.html whenever the 'Next' button pressed. This 
gets all inputs from HTML form and stores them in the spreadsheet.

Parameters:
inputArray: a 1x3 array which contains the owner, reference and type of scale.
*/

function addProjectInputs(inputArray) {
  var sheetName = "Inputs";
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getSheetByName(sheetName);
  Logger.log(sheet);
  var d = new Date();
  var tz = spreadsheet.getSpreadsheetTimeZone();
  var d = [Utilities.formatDate(d,tz,  "dd/MM/yyyy")];
  var data = d.concat(inputArray);
  Logger.log(data);
  sheet.appendRow(data);
}

/* Function to append further data onto the row created from 
addProjectInputs() */

function addFurtherInputs(idlerArray) {
  var sheetName = "Inputs";
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getSheetByName(sheetName);

  var lastRow = sheet.getLastRow();
  var lastColumn = sheet.getLastColumn()-42;

  var arrayEmpty = idlerArray.every(function(i) { return i === ''; });
  Logger.log(arrayEmpty);

  if (arrayEmpty !== true) {
    for (i = 0; i < idlerArray.length; i++) {
      var range3 = sheet.getRange(lastRow,lastColumn+1+i,1,1);
      range3.setValue(idlerArray[i]);
    }
  }
}

The summary page of the app (page 5, appears after all writing steps) then reads this data (using .getRange().getValues()) and then displays them on the page.

This works fine when one user is on it - however this app will get used by up to 20 people, potentially concurrently. This is an issue. For example, if a second user were to start using the app, their data would then start overwriting the first user's by adding a new row, thus 'archiving' the first user's data. I've come across the following potential solutions:

  1. Making use of the LockService to force only one person to use the app at a time (not ideal since the app would be used for quoting customers and don't really want to keep users waiting).
  2. Multithreading
  3. Stacks

I'm not sure how 2 or 3 would work, or even possible in GAS. Could anyone please shed some light on if it is possible to facilitate multiple users, and how?

Thanks in advance

解决方案

Rather than locking, I would approach this by generating a unique ID for each user session, which you can store in the sheet and use to match up the values later.

On your entry point [probably doGet(), maybe doPost()], generate a UUID with var session_id = Utilities.getUuid();

Then pass that sessiond ID to your template, and include it in all future requests back to the Apps Script.

Write the UUID to a column in the sheet in your addProjectInputs() function, then modify your addFurtherInputs() function to locate the correct row based on the UUID, (rather than always taking the last one) and write your updates to that row.

This approach guarantees you can have many users working without worrying about overwriting each other, and with no delays due to locks.

See https://developers.google.com/apps-script/reference/utilities/utilities#getUuid()

这篇关于如何在Google Apps脚本Webapp上为并发用户提供便利?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆