上传文件到Google Drive从Google表单文件上传重命名与字段名(字符串值)从表单响应收集?



这段代码目前从表单响应中获取唯一ID,并将其作为所有上传文件所在的文件夹名称。我想更改代码,以便它从Google Form响应中获取有组织视图的文本字段,例如First Name_Last Name。

const PARENT_FOLDER_ID = "2ih2384723847234878h23jkbi2j3b4ijb23i4bhwed34f";
const initialize = () => {
const form = FormApp.getActiveForm();
ScriptApp.newTrigger("onFormSubmit").forForm(form).onFormSubmit().create();
};
const onFormSubmit = ({ response } = {}) => {
try {
// Get a list of all files uploaded with the response
const files = response
.getItemResponses()
// We are only interested in File Upload type of questions
.filter(
(itemResponse) =>
itemResponse.getItem().getType().toString() === "FILE_UPLOAD"
)
.map((itemResponse) => itemResponse.getResponse())
// The response includes the file ids in an array that we can flatten
.reduce((a, b) => [...a, ...b], []);
if (files.length > 0) {
// Each form response has a unique Id
const subfolderName = response.getId();
const parentFolder = DriveApp.getFolderById(PARENT_FOLDER_ID);
const subfolder = parentFolder.createFolder(subfolderName);
files.forEach((fileId) => {
// Move each file into the custom folder
DriveApp.getFileById(fileId).moveTo(subfolder);
});
}
} catch (f) {
Logger.log(f);
}
};

假设名字是第一个和第二个问题。

/* ... */
const n = response.getItemResponses().slice(0, 2)
const fn = n[0].getResponse();
const ln = n[1].getResponse();
/* ... */
const subfolderName = `${fn}_${ln}`;
/* ... */

最新更新