Python调用Google Apps Script写入Google Docs返回404错误求助
问题描述
我正在制作自定义Google Docs模板,打算用Python脚本爬取的数据自动填充内容。目前已经完成模板创建和目标数据爬取,但调用Google Apps Script传递数据时,收到404错误提示:Error: Requested entity was not found.。
我猜测问题出在Python脚本的参数传递逻辑或者Google Apps Script的函数实现上,所有权限和凭证都已配置完成,为保护隐私省略了脚本及文档ID,希望能得到解决指导。相关代码如下:
Google Apps Script代码
var sourceDoc = DocumentApp.openById("id of template doc").getBody(); var targetDoc = DocumentApp.openById("id of final doc"); var body = targetDoc.getBody(); function scrapetoDoc(e) { Logger.log(e) var company = e.parameter.company; var product = e.parameter.product; var model = e.parameter.model; var date = e.parameter.date; copyDoc(); body.replaceText("{{Date}}", date); body.replaceText("{{Company}}", company); body.replaceText("{{Product}}", product); body.replaceText("{{Model}}", model); targetDoc.saveAndClose(); } function copyDoc() { var totalElements = sourceDoc.getNumChildren(); for( var j = 0; j < totalElements; ++j ) { var element = sourceDoc.getChild(j).copy(); var type = element.getType(); if( type == DocumentApp.ElementType.PARAGRAPH ){ body.appendParagraph(element); } else if( type == DocumentApp.ElementType.TABLE){ body.appendTable(element); } else if( type == DocumentApp.ElementType.LIST_ITEM){ body.appendListItem(element); } } }
Python主函数代码
try: service = build('script', 'v1', credentials=creds) request = { "function": "scrapetoDoc", "parameters": [{ "company": scrapped_data[0], "product": scrapped_data[1], "model": scrapped_data[2], "date": scrapped_data[4]}] } # Make the API request. response = service.scripts().run( body=request, scriptId=SCRIPT_ID ).execute() except errors.HttpError as error: # The API encountered a problem. print(error.content)
解决指导
1. 排查文档ID正确性
Requested entity was not found 最常见原因是文档ID错误:
- 确认
sourceDoc和targetDoc的ID是否正确,检查拼写、空格或复制完整性。 - 确保脚本运行账号有权访问这两个文档(已配置共享权限)。
2. 修正参数传递与接收逻辑
Python传递的是对象参数,但GAS中e.parameter仅适用于Web App的GET/POST请求,通过Apps Script API调用时需直接接收参数:
- 修改GAS的
scrapetoDoc函数:
function scrapetoDoc(params) { Logger.log(params) var company = params.company; var product = params.product; var model = params.model; var date = params.date; // 后续逻辑不变 }
- 确认Python中
scrapped_data[4]是有效日期值,避免空值引发异常。
3. 调整文档初始化时机
全局作用域初始化文档可能导致权限或缓存问题,建议移到函数内部:
function scrapetoDoc(params) { var sourceDoc = DocumentApp.openById("id of template doc").getBody(); var targetDoc = DocumentApp.openById("id of final doc"); var body = targetDoc.getBody(); Logger.log(params) var company = params.company; var product = params.product; var model = params.model; var date = params.date; copyDoc(sourceDoc, body); body.replaceText("{{Date}}", date); body.replaceText("{{Company}}", company); body.replaceText("{{Product}}", product); body.replaceText("{{Model}}", model); targetDoc.saveAndClose(); } function copyDoc(sourceDoc, body) { var totalElements = sourceDoc.getNumChildren(); for( var j = 0; j < totalElements; ++j ) { var element = sourceDoc.getChild(j).copy(); var type = element.getType(); if( type == DocumentApp.ElementType.PARAGRAPH ){ body.appendParagraph(element); } else if( type == DocumentApp.ElementType.TABLE){ body.appendTable(element); } else if( type == DocumentApp.ElementType.LIST_ITEM){ body.appendListItem(element); } } }
4. 检查Apps Script API配置
- 确认Python代码中的
SCRIPT_ID是GAS项目的脚本ID(而非文档ID),且完全正确。 - 检查GAS项目是否已部署为API可执行,同时在Google Cloud Console中确认Apps Script API已启用。
内容的提问来源于stack exchange,提问作者dan.c
相关产品推荐
相关产品推荐

