我正在发送包含 Google 电子表格和 Google 脚本的自动报告。
到目前为止,它运行得很好。但不知何故,当我尝试创建要通过电子邮件发送的新报告时,函数“UrlFetchApp.fetch”返回 404。当我尝试复制旧报告时,也发生了同样的情况。
带有“UrlFetchApp.fetch”的行给我这个错误:
Request failed for https://docs.google.com/spreadsheets/d/1qm_bCKn4MbLKy7AIuIeu7bTZcTk8ObYBln0GAxwfsX8/pub?gid=195635557&single=true&output=pdf returned code 404. Truncated server response
看来我不是唯一遇到这个问题的人,但我找不到任何解决方案。
这是代码:
function emailSpreadsheetAsCSV() {
var ss = SpreadsheetApp.openById("1qm_bCKn4MbLKy7AIuIeu7bTZcTk8ObYBln0GAxwfsX8");
var url = ss.getUrl();
url = url.replace(/edit$/,'');
var token = ScriptApp.getOAuthToken();
var sheets = ss.getSheets();
//make an empty array to hold your fetched blobs
var blobs = [];
for (var i=0; i<sheets.length; i++) {
var response = UrlFetchApp.fetch("https://docs.google.com/spreadsheets/d/1qm_bCKn4MbLKy7AIuIeu7bTZcTk8ObYBln0GAxwfsX8/pub?gid=195635557&single=true&output=pdf", {
headers: {
'Authorization': 'Bearer ' + token
},
'muteHttpExceptions': false
});
//convert the response to a blob and store in our array
blobs[i] = response.getBlob().setName(sheets[i].getName() + '.csv');
}
//create new blob that is a zip file containing our blob array
var zipBlob = Utilities.zip(blobs).setName(ss.getName() + '.zip');
return blobs[0];
}
非常感谢您的帮助。
艾默里克。
最佳答案
我也遇到了这个问题,经过一番研究,Spence Easton(问题 34573295)解决了我的问题。我只是将这个虚假调用添加到驱动器中,以便授予访问权限,因此脚本现在可以访问您的文件。 在尝试获取 url 之前将其添加到顶部附近:
var bogus = DriveApp.getRootFolder();
现在运行正常,没有出现 404 错误。
关于google-apps-script - 即使页面存在,Google 脚本 UrlFetchApp.fetch 返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32477504/