我需要一个脚本帮助,将 Google 表单的每个响应保存在我的 Google 云端硬盘中的单独 TXT 文件中。我被困在第 3 行:我在
var responses = e.values
中遇到错误
Debug:
TypeError: Cannot read properties of undefined (reading 'values')
onFormSubmit @ Código.gs:3
当之无愧的结果:
Google 云端硬盘上的相同 Google 表单 -> QuestionA -> ResponseA -> ResponseA.txt
Google 云端硬盘上的相同 Google 表单 -> QuestionB -> ResponseB -> ResponseB.txt
我使用的脚本:
function onFormSubmit(e) {
// To get form responses
var responses = e.values;
// Google Drive ID Folder
var folder = DriveApp.getFolderById('PASTA_ID');
// Each Field saving in separated txt file
for (var i = 1; i < responses.length; i++) {
var response = responses[i];
// File name based in the field
var fileName = "resposta_campo_" + i + ".txt";
// Check if the file exists
var existingFiles = folder.getFilesByName(fileName);
// If files exists, delete
if (existingFiles.hasNext()) {
var existingFile = existingFiles.next();
existingFile.setTrashed(true); //move files to trash
}
// New file with the new information
folder.createFile(fileName, response);
}
}
我将非常感谢任何帮助。
您想要将对问题的回答写入特定的文本文件。
例如在给定的 Google 表单中:
如何通过谷歌脚本更新谷歌驱动器文本文件?中的问题与这个问题非常相似,尽管存在一些关键差异。考虑这个答案,它改编自 @RyanDugan 在参考问题中的答案。
逻辑
onFormSubmit
var questions = ["Question A", "Question B"]
=- 标识特定问题的标题,因此用于:
e.namedValues[questions[i]]
循环浏览问题、获取问题答案并识别文件名。+"\n"
在每个响应之间创建一个新的行分隔符,从而提高文本文件的可读性。function copyResp2Txt(e) {
// Logger.log(JSON.stringify(e)) // DEBUG
var folderId = "<<insert Folder ID>>"
var folder = DriveApp.getFolderById(folderId)
// does folder exist
if (folder) {
// found matching folder
var questions = ["Question A", "Question B"]
for (var i=0;i<questions.length;i++){
var content = e.namedValues[questions[i]]+"\n"
// search for files with matching name
var fileList = folder.getFilesByName(questions[i]);
if (fileList.hasNext()) {
// found matching file - append text
var file = fileList.next();
var combinedContent = file.getBlob().getDataAsString() + content;
file.setContent(combinedContent);
}
else {
// file not found - create new
folder.createFile(questions[i], content);
}
}
}
}