表单上的应用程序脚本如何将额外数据存储到工作表中?

use*_*146 7 google-apps-script google-forms

问:附加到表单的AppsScript如何将额外的数据存储到工作表中?

情况:我们有一个(长)Google表单,可以将许多数据存储到Google表格中.通常需要编辑条目,并且使用原始表单编辑比尝试直接编辑到工作表更容易.(有些项目是文本,有几段长.)我想在电子表格中存储一个额外的数据,特别是编辑器可以使用该表格编辑行条目的URL.

我已经可以获取所有表单数据,并且可以使用formResponse.getEditResponseUrl()获取正确的URL.我可以通过电子邮件将所有这些内容发送给用户,通常是收集所有表单条目的编辑.(感谢StackOverflow中的许多有用的答案让我这么做!)但是编辑器必须手动将URL复制并粘贴到电子表格的正确行中的附加列中.

我在类Sheet中看到一个接口,用于向电子表格添加一列,但是我没有看到如何填充表单刚刚存储的特定行的额外列.我们已手动添加了该列,并确认在通过表单进行编辑时Google不会覆盖该列.如何将一小段数据存储到工作表中?

我错过了什么?任何帮助将不胜感激.谢谢.

[补充说明2015-02-06]

  • 我们有一个很长的表格,有些人提交,其他人编辑.编辑将使用表单完成,而不是直接在电子表格中编辑,因此我们需要允许编辑者重新编辑响应的URL.

  • 我希望在表单提交期间将该URL存储到电子表格中,以便有权访问该表单的编辑者可以找到它.

  • 在表单端的脚本中,我可以轻松地计算该URL,但现在如何将其存储在工作表中的额外列中?

  • 在我目前的表单端脚本中,我获取URL并将其与所有表单数据一起发送到编辑器的分发列表的电子邮件中.然后,其中一个编辑器从电子邮件中复制URL并将其粘贴到工作表中.(大多数情况下,进入正确的行,甚至.:-)这是一个可能容易出错的手动步骤.)

  • 第二个问题:表单中的行号与form.getResponses()中的响应数字有什么关系?当提交新项目(即新行)时,行号和响应号似乎会漂移,并且编辑旧项目.可以合理地预测编辑器将找到表单数据的工作表行号吗?

再次感谢您提供的任何帮助.我们有一个可生存的临时解决方案.但是,在接下来的几个月里有大约一百个表格条目,我希望尽可能地对这个过程进行错误验证.

干草堆

Teo*_*ite 5

因此,我偶然发现了您的问题,希望我已经正确理解了。

可能的问题:

  • 脚本错误地绑定到了表单所附的电子表格,而不是表单本身(据您的描述,就您而言,这不是问题)

  • 提交插入与附加列编辑之间或同时提交之间的竞争条件(请参阅代码的第27-32行)

  • 直接访问电子表格,而无需事先从电子表格中选择工作表,即使该电子表格仅包含一张工作表也是如此!(请参阅代码的第36-37行)

  • 使用列数字索引,而不是使用对应的列字母作为getRange()方法的参数,该方法仅接受列字母AFAIK(请参阅代码的第42-43行)

下面有应解决所有这些问题的代码(我没有测试过,但是它是针对非常相似的情况的完美工作解决方案的改编):

// Converts sheet column numeric index to corresponding column letter
function columnToLetter(column)
{
  var temp, letter = '';
  while (column > 0)
  {
    temp = (column - 1) % 26;
    letter = String.fromCharCode(temp + 65) + letter;
    column = (column - temp - 1) / 26;
  }
  return letter;
}
Run Code Online (Sandbox Code Playgroud)

必须将以下功能注册到表单(而非电子表格)的“表单提交”事件中!(脚本工具栏->资源->当前项目的触发器->添加新触发器)

// Associated the sheet rows with response URLs in an additional column
function onFormSubmit(e)
{
  try
  {
    // Get the response Url, either from FormApp:
    var responseUrl = FormApp.getActiveForm().getEditResponseUrl();
    // Or alternatively get it from the event:
//  var responseUrl e.response.getId().getEditResponseUrl();
    // ....................
    // Other URL processing
    // ....................

    // Get a public lock on this script, because we're about to modify a shared resource.
    var lock = LockService.getPublicLock();
    // Wait for up to 30 seconds for other processes to finish.
    lock.waitLock(30000);
    // Wait for row insertion to finish, so that sheet.getLastRow() method gets the updated number of rows
    Utilities.sleep(1000); // 1 second

    // Here insert the URL to your spreadsheet
    var spreadsheetUrl = "https://docs.google.com/spreadsheets/d/YGUgHi28_gYUffGYGGH_78hkO1Pk/edit";
    // Gets the first sheet inside the spreadsheet (if you have multiple sheets, just change the value [0])
    var sheet = SpreadsheetApp.openByUrl(spreadsheetUrl).getSheets()[0];
    // Get updated number of rows and columns, after form submit inserted the new row
    var lastRow = sheet.getLastRow();
    var lastColumn = sheet.getLastColumn();

    // Get the exact cell, next to the right of the new row, by converting the column index to corresponding letter
    var lastCell = columnToLetter(lastColumn) + lastRow.toString();
    // Set the content of the cell with the new URL
    sheet.getRange(lastCell).setValue(responseUrl);

    // Release the lock so that other processes can continue.
    lock.releaseLock();    
  }
  catch (error)
  {
    // If there's an error, show the error message
    return error.toString();
  }
}
Run Code Online (Sandbox Code Playgroud)

对于其他任何问题,只需写评论。希望能帮助到你。


Cyr*_*ree 1

您可以使用表单提交范围参数来获取放置在工作表中的表单数据的行/电子表格范围。然后使用范围偏移方法将数据推入表单数据最后一列之后的列。

请注意,如果您使用 HYPERLINK 公式,则必须对作为参数传递的引号进行转义。

例如

function formProcessing(e){
  var formData = e.values;
  var dataRange = e.range; // gets the range on the spreadsheet
  /*
   do all your processing


 */
  var url = "http://www.google.com"; // whatever url to put in spreadsheet

  // add the url value to the spreadsheet
   formRange.getCell(1,formRange.getLastColumn()).offset(0,1).setValue(url);
    // or if you want a named link
  //formRange.getCell(1,formRange.getLastColumn()).offset(0,1).setFormula("HYPERLINK(\"" + url + "\", \"Edit Form\")");
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在此输入图像描述