Testar implantação funciona corretamente.
A pasta acessada bem como todos os arquivos dentro dela estão compartilhados com qualquer pessoa com o link.
const pastaCertificados = DriveApp.getFolderById('escondi');
const sheet = SpreadsheetApp.openById('1L2G-escondi').getActiveSheet();
const dados = sheet.getDataRange().getValues();
const templateFile = DriveApp.getFileById('escondi');
console.log('carregando dados...');
const participantesCache = dados.slice(2).map(row => ({
nome: row[0], // Coluna A (índice 0)
frequencia: row[8] // Coluna I (índice 8)
}));
// Renderizar a interface
function doGet() {
return HtmlService.createHtmlOutputFromFile('index').setTitle('Certificados');
}
// Função para retornar apenas a lista de nomes
function getNomes() {
return participantesCache.map(p => p.nome);
}
function gerarCertificado(nome) {
// Busca o participante na cache
const participante = participantesCache.find(p => p.nome === nome);
console.log('participante selecionado:', participante);
if (!participante) {
throw new Error("Participante não encontrado.");
}
else {
// Converter o nome para maiúsculas
nomeDoParticipante = participante.nome.toUpperCase();
// Verifica se o PDF já existe na pasta
const existingFiles = pastaCertificados.getFilesByName(`Certificado - ${nomeDoParticipante}.pdf`);
if (existingFiles.hasNext()) {
const existingFile = existingFiles.next();
console.log('PDF já existe:', existingFile.getUrl());
return existingFile.getUrl(); // Retorna o link do PDF existente
}
else {
// Cria uma cópia do modelo
const certificado = templateFile.makeCopy('Certificado - ' + nomeDoParticipante);
// Abre o documento e faz as substituições necessárias
const doc = DocumentApp.openById(certificado.getId());
const body = doc.getBody();
body.replaceText('<<NOME_DO_PARTICIPANTE>>', nomeDoParticipante);
body.replaceText('<<FREQUENCIA>>', (participante.frequencia * 100).toFixed(0) + '%');
doc.saveAndClose();
// Gera o PDF e retorna o link
const pdf = DriveApp.getFileById(certificado.getId()).getAs('application/pdf');
const pdfFile = pastaCertificados.createFile(pdf).setName('Certificado - ' + nome + '.pdf');
// Configurar compartilhamento público do PDF
pdfFile.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
// Opcional: Remove o documento do Google Docs, se não precisar mais
DriveApp.getFileById(certificado.getId()).setTrashed(true);
return pdfFile.getUrl(); // Retorna o link do PDF
}
}
}