JSON.parse不适用于url链接

时间:2016-11-01 14:15:45

标签: javascript json url google-apps-script

我是初级javascript / google-apps-script开发人员,我想在Google表格的某些工作表中添加一些功能。我正在处理许多网址,需要跟踪作者上次修改的时间。

我已经构建了一些我认为可行的脚本,但显然(经过一些阅读)需要一些专业的触摸。

想法是遍历一列URL(2500~)并将每个URL的修改日期(从其元数据)输出到右侧的单元格中。这是我的代码:

    function iteration1() {
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        **//The list to iterate on.**
        var sheet = ss.getSheetByName("Fund List");
        **//The column of which the links are stored**
        var urls = sheet.getRange("D2:D150").getValues();

        for (var row = 0; row < urls.length; row++) {
            for (var col = 0; col < urls[row].length; col++)
                **//Varifying if there is a URL within the cell**
                if (urls[row][col] != '') {
                    **//Storing each URL in a new array**
                    var url = UrlFetchApp.fetch(urls[row][col].valueOf());
                    **//Parsing the meta-data of the URL into an array**
                    var tweets = JSON.parse(url);
                    **//Retrieve the link modification date from the meta-data array & outputs to the cell from the right respectivley.**
                    sheet.getRange(row+2, 13).setValue(Logger.log(tweets[4][2]).getLog());
            }
        }
    }

例如:链接http://documents.financialexpress.net/Literature/37773008.pdf

其元数据是:

{Accept-Ranges=bytes, X-Robots-Tag=noindex, nofollow, noarchive,nosnippet, Cache-Control=max-age=604800, Server=Microsoft-IIS/7.0, ETag="01827159b1d11:0", Access-Control-Allow-Origin=*, Access-Control-Allow-Methods=GET,PUT,POST,DELETE,OPTIONS, Last-Modified=Wed, 18 May 2016 23:00:00 GMT, Content-Length=113029, Access-Control-Allow-Headers=Content-Type, Date=Thu, 01 Sep 2016 11:43:52 GMT, Content-Type=application/pdf}

我只需要在此元数据数组中使用Last-Modified字段Date并将其从右侧输出到单元格。

预先感谢帮助者!这里很棒的社区!

我添加了当前代码和调试器模式的屏幕截图,其中给出了我正在处理的链接的示例: enter image description here

2 个答案:

答案 0 :(得分:0)

根据我在google文档(https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String))中看到的结果,您存储在变量 url 中的结果不是字符串。

JSON.parse接受一个字符串并将其转换为javascript Object / Array / String / whatever

您需要使用而不是JSON.parse(url),JSON.parse(url.getContentText(&#39; utf-8&#39;)),如下所示:https://developers.google.com/apps-script/reference/url-fetch/http-response

答案 1 :(得分:0)

经过几天的努力,我已经设法为我的工作表中的每个网址检索Last-Modified日期密钥的值。

我的代码:

function iteration1() { 
  var ss = SpreadsheetApp.getActiveSpreadsheet();
    //The Google sheet to access
    var sheet = ss.getSheetByName("Sheet Name");
    //The array of URLs to check
    var urls = sheet.getRange("D2:D150").getDisplayValues();

    for (var row = 0; row < urls.length; row++) {
      for (var col = 0; col < urls[row].length; col++) {
        if (urls[row][col].toString() != '') {
          //Converting each URL to string and retrieving its Properties into a new Array
          var url = UrlFetchApp.fetch(urls[row][col].toString());
          var tweets = url.getAllHeaders();

          //Forming an array of Properties by Keys & Values
          var userProperties = PropertiesService.getUserProperties();
          userProperties.setProperties(tweets);
          var tweetsKeys = Object.keys(tweets);
        }
      }

      //Retrieving the link modification date from the property meta-data & outputs it as a String to the cell from the right respectivley.
      sheet.getRange(row+2, 12).setValue(userProperties.getProperty(tweetsKeys[7]));
    }
}

非常感谢您的回复!

相关问题