如何将响应代码拖入Google电子表格中 [英] How to pull a response code into Google Spreadsheet

查看:149
本文介绍了如何将响应代码拖入Google电子表格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个URL列表,我想检查响应代码。有谁知道我可以在Google文档中做到这一点?我有16000个网址,所以这是一个很大的问题。



任何帮助表示赞赏。

解决方案

没有内建公式函数可以做到这一点,但您可以在 Google Apps脚本。这里有一个例子,你可以适应你的目的。

 函数HTTPResponse(uri)
{
var response_code;
尝试{
response_code = UrlFetchApp .fetch(uri).getResponseCode().toString();

catch(error){
response_code = error .toString().match(/ returned code(\d\d\d)\。/)[1] ;
}
finally {
return response_code;






$ b

定义了这个函数后,你可以从cell:

  A1:'http://www.google.com/ 
A2:= HTTPResponse(A1)

UrlFetchApp.fetch()使字符串匹配成为必需。在讨论主题中承诺修正 UrlFetchApp.fetch - 获取异常的响应代码? ,但我找不到它曾经交付过。解决方法取决于异常消息的文本。这看起来不可靠,依赖于语言环境。



Google的文章 故障排除和调试脚本 警告Google Apps脚本会对各种操作强加限额。您可能会发现同时有16,000个HTTP请求会被限制回来。本文将具有特殊需求的用户引用到 Google Apps脚本帮助论坛


I have a list of URL's that I want to check the response code for. Does anyone know how I could do this in Google docs? I have 16000 url's so it's quite a big ask.

Any help appreciated.

解决方案

There is no builtin formula function to do this, but you can write one in Google Apps Script. Here is an example that you can adapt to your purpose.

function HTTPResponse( uri )
{
  var response_code ;
  try {
    response_code = UrlFetchApp .fetch( uri ) .getResponseCode() .toString() ;
  }
  catch( error ) {
    response_code = error .toString() .match( / returned code (\d\d\d)\./ )[1] ;
  }
  finally {
    return response_code ;
  }
}

Having defined this function, you can call it from a cell:

A1: 'http://www.google.com/
A2: = HTTPResponse( A1 )

A bug in UrlFetchApp.fetch() makes the string matching necessary. A fix was promised in the discussion thread UrlFetchApp.fetch - get response code on exception?, but I cannot find that it was ever delivered. The workaround is dependent on the text of the exception message. That seems unreliable and dependent on locale.

Google's article Troubleshooting and Debugging Scripts warns that Google Apps Script imposes quotas on various actions. You might well find that 16,000 simultaneous HTTP requests will be throttled back. The article refers users having special needs to the Google Apps Script Help Forum.

这篇关于如何将响应代码拖入Google电子表格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆