我是 GAS 的新人,我正在关注 Youtube 上的教程,以了解它如何在 Google Workspace 的不同平台上运行。但是,当我在 Google Sheets 中执行代码时,它提示我这个错误:
TypeError:无法读取 null 的属性(读取“getBody”)
function InsertarDatos() {
var DocActual = DriveApp.getFileById("1kM-zglMjbckKnacmV5K21j1r3CjDmn70yrzsCcKrNdw");
var fila = 2;
var nombreCelda = "A" + fila;
var hojaActual = SpreadsheetApp.getActive();
var celdaActual = hojaActual.getRange(nombreCelda);
while (!celdaActual.isBlank()) {
var DocNuevo = DocActual.makeCopy("Nombre: " + hojaActual.getRange("A" + fila).getValue());
var documento = DocumentApp.getActiveDocument();
var fecha = new Date();
var mes = fecha.getMonth() + 1;
var dia = fecha.getDate();
var ano = fecha.getFullYear();
var fechaT = "Certificado emitido el día " + dia + "del mes " + mes + " de " + ano + ".";
documento.getBody().replaceText("<<nombre>>", hojaActual.getRange("A" + fila).getValue());
documento.getBody().replaceText("<<dni>>", hojaActual.getRange("B" + fila).getValue());
documento.getBody().replaceText("<<curso>>", hojaActual.getRange("C" + fila).getValue());
documento.getBody().replaceText("<<empresa>>", hojaActual.getRange("D" + fila).getValue());
documento.getBody().replaceText("<<calific>>", hojaActual.getRange("E" + fila).getValue());
documento.getBody().replaceText("<<fecha>>", fechaT);
fila++;
nombreCelda = "A" + fila;
celdaActual = hojaActual.getRange(nombreCelda);
}
}
我预计它会创建多个文档来替换我将放入 Google Sheets 数据库中的不同项目。预期的文档
DocumentApp.getActiveDocument()
返回 null,因为 Google Apps 脚本项目包含在电子表格中。
您可以将其替换为
DocumentApp.openById(document_id)
或 DocumentApp.openByUrl(document_url)
。您应该分别将 document_id / document_url 替换为保存文档 id / URL 的字符串,或者将此字符串分配到的变量。