0% found this document useful (0 votes)
2 views

Script - Forms - Mad

The script processes form responses to extract a CPF and any uploaded file IDs. It checks for the existence of the CPF and uploaded files, creating a new folder named after the CPF in a specified parent folder. If a folder with the same name exists, it appends a number to the name and moves the uploaded files into the newly created folder.

Uploaded by

hoc.rh.contec
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Script - Forms - Mad

The script processes form responses to extract a CPF and any uploaded file IDs. It checks for the existence of the CPF and uploaded files, creating a new folder named after the CPF in a specified parent folder. If a folder with the same name exists, it appends a number to the name and moves the uploaded files into the newly created folder.

Uploaded by

hoc.rh.contec
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

const formResponse = e.

response;
const itemResponses = formResponse.getItemResponses();

let cpf = '';


let fileIds = [];

itemResponses.forEach(response => {
const item = response.getItem();
const title = item.getTitle();

Logger.log("Título: " + title);


Logger.log("Resposta: " + response.getResponse());

if (title === "CPF") {


cpf = response.getResponse();
} else if (item.getType() === FormApp.ItemType.FILE_UPLOAD) {
const uploadedFiles = response.getResponse();
if (Array.isArray(uploadedFiles)) {
fileIds = fileIds.concat(uploadedFiles);
} else {
fileIds.push(uploadedFiles);
}
Logger.log("IDs dos arquivos para o título '" + title + "': " +
JSON.stringify(fileIds));
}
});

if (!cpf) {
Logger.log("CPF não encontrado.");
return;
}

if (!fileIds || fileIds.length === 0) {


Logger.log("Nenhum arquivo enviado.");
return;
}

// Criar ou obter a pasta do CPF


const parentFolderId = 'AQUI'; // ID da pasta pai
const parentFolder = DriveApp.getFolderById(parentFolderId);

let cpfFolder;
let folderName = cpf;
let folderCount = 1;

// Verificar se a pasta já existe e criar um novo nome se necessário


while (parentFolder.getFoldersByName(folderName).hasNext()) {
folderName = cpf + " (" + folderCount + ")";
folderCount++;
}
// Criar a nova pasta com o nome adequado
cpfFolder = parentFolder.createFolder(folderName);
Logger.log("Pasta criada: " + cpfFolder.getName());

// Mover os arquivos para a nova pasta do CPF


fileIds.forEach(fileId => {
const file = DriveApp.getFileById(fileId);
file.moveTo(cpfFolder);
Logger.log("Arquivo movido: " + file.getName() + " para a pasta: "
+ cpfFolder.getName());
});
}

You might also like