问:附加到表单的 AppsScript 如何将额外的数据存储到工作表中?
情况:我们有一个(长)谷歌表单,它将许多数据存储到一个谷歌表格中。通常需要编辑条目,并且使用原始表单进行编辑比尝试直接在工作表中进行编辑要容易得多。 (其中一些项目是文本,有几段长。)我想在电子表格中存储一条额外的数据,特别是编辑器可以用来使用表单编辑行条目的 URL。
我已经可以获取所有表单数据,并且可以使用 formResponse.getEditResponseUrl() 获取正确的 URL。我可以将所有这些通过电子邮件发送给用户,通常是收集所有表单条目的编辑。 (感谢 StackOverflow 中的许多有用答案让我走到了这一步!)但是编辑器必须手动将 URL 复制并粘贴到电子表格适当行中的附加列中。
我在 Sheet 类中看到了一个向电子表格添加列的界面,但我看不到如何为表单刚刚存储的特定行填充额外的列。我们已经手动添加了该列,并验证了在通过表单进行编辑时它没有被 Google 覆盖。如何将一小段数据存储到工作表中?
我错过了什么?任何帮助将不胜感激。谢谢。
[补充说明 2015-02-06]
再次感谢您在这方面给我的任何帮助。我们有一个可行的临时解决方案。然而,在接下来的几个月里,会有大约一百个表单条目出现,我希望尽可能地对这个过程进行防错。
瑞克
最佳答案
所以,我刚刚偶然发现了你的问题,希望我理解正确。
可能出现的问题:
下面你有应该解决所有这些问题的代码(我没有测试过,但它是一个非常相似的场景的完美工作解决方案的改编):
// 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;
}
以下功能必须从表单注册到“表单提交”事件 - 而不是电子表格! (脚本工具栏 -> 资源 -> 当前项目的触发器 -> 添加新触发器)
// 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();
}
}
对于任何其他问题,只需写评论。希望能帮助到你。
关于google-apps-script - 表单上的应用程序脚本如何将额外数据存储到工作表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28143036/