Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 将PubMed数据拉入Google表单_Google Apps Script_Google Sheets_Pubmed - Fatal编程技术网

Google apps script 将PubMed数据拉入Google表单

Google apps script 将PubMed数据拉入Google表单,google-apps-script,google-sheets,pubmed,Google Apps Script,Google Sheets,Pubmed,我在找人帮忙。我正试图从PubMed获取一位作者的出版物,并使用Apps脚本将数据填充到Google表单中。我已经得到了下面的代码,现在卡住了 基本上,我所做的是首先从一个特定的作者那里提取所有的Pubmed ID,这个作者的名字来自于工作表的名字。然后我尝试创建一个循环来遍历每个Pubmed ID JSON摘要,并提取我想要的每个字段。我已经可以在酒吧约会了。我设置它的想法是,我将为我想要的PMID的每个字段执行一个循环,将其存储在一个数组中,然后将其返回到我的工作表中。但是,我现在无法获取第

我在找人帮忙。我正试图从PubMed获取一位作者的出版物,并使用Apps脚本将数据填充到Google表单中。我已经得到了下面的代码,现在卡住了

基本上,我所做的是首先从一个特定的作者那里提取所有的Pubmed ID,这个作者的名字来自于工作表的名字。然后我尝试创建一个循环来遍历每个Pubmed ID JSON摘要,并提取我想要的每个字段。我已经可以在酒吧约会了。我设置它的想法是,我将为我想要的PMID的每个字段执行一个循环,将其存储在一个数组中,然后将其返回到我的工作表中。但是,我现在无法获取第二个字段--
标题
——以及所有后续字段(例如作者、最后一位作者、第一位作者等)

任何帮助都将不胜感激

function IMPORTPMID(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];                   
  var author = sheet.getSheetName();

  var url = ("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=" + author + "[author]&retmode=json&retmax=1000");
  var response = UrlFetchApp.fetch(url);
  var AllAuthorPMID = JSON.parse(response.getContentText());

  var xpath = "esearchresult/idlist";
  var patharray = xpath.split("/");
  for (var i = 0; i < patharray.length; i++) {
     AllAuthorPMID = AllAuthorPMID[patharray[i]];
  }

  var PMID = AllAuthorPMID;
  var PDparsearray = [PMID.length];
  var titleparsearray = [PMID.length];
  for (var x = 0; x < PMID.length; x++) {
    var urlsum = ("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&retmode=json&rettype=abstract&id=" + PMID[x]);
    var ressum = UrlFetchApp.fetch(urlsum);
    var contentsum = ressum.getContentText();
    var jsonsum = JSON.parse(contentsum);

    var PDpath = "result/" + PMID[x] + "/pubdate";
    var titlepath = "result/" + PMID[x] + "/title";
    var PDpatharray = PDpath.split("/");
    var titlepatharray = titlepath.split("/");

    for (var j = 0; j < PDpatharray.length; j++) {
      var jsonsum = jsonsum[PDpatharray[j]];
    }  

    PDparsearray[x] = jsonsum;
  }

  var tempArr = [];
  for (var obj in AllAuthorPMID) {
    tempArr.push([obj, AllAuthorPMID[obj], PDparsearray[obj]]);
  }

  return tempArr;
}
函数importpid(){
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getSheets()[0];
var author=sheet.getSheetName();
变量url=(“https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=“+author+”[author]&retmode=json&retmax=1000”);
var response=UrlFetchApp.fetch(url);
var AllAuthorPMID=JSON.parse(response.getContentText());
var xpath=“esearchresult/idlist”;
var patharray=xpath.split(“/”);
对于(变量i=0;i
通过对给定PubMed ID的PubMed JSON响应,您应该能够确定要包含在摘要报告中的字段名(及其路径)。如果它们都处于同一级别,那么读取它们更容易实现,但是如果其中一些是子字段的属性,那么如果您在设置中提供了正确的路径,您仍然可以访问它们

考虑“源JSON”:

publid
issn
字段将处于同一级别,而
publications
authors
不会处于同一级别

通过1)硬编码字段访问,或2)编写解析字段路径并提供字段路径的代码,可以在同一循环中检索
pubMedId
publications
字段(以及您需要的其他字段)

选项1可能会更快,但如果您突然想要获得一个新字段,则灵活性会大大降低,因为您必须记住如何编写代码来访问该字段,以及在何处插入该字段,等等。如果API发生变化,上帝保佑您

选项2很难正确,但一旦正确,将(应该)适用于您(正确)指定的任何字段。获取一个新字段就像在相关的配置变量中写入它的路径一样简单。可能有一些库可以为您这样做

将上述转换为电子表格行(在外部数组中,例如每一个代码> PUBMEMEDD ,例如查询API的ID),请考虑如下:

执行此操作将在Stackdriver中产生以下输出:

显然,您可能需要一些不同的(也称为real)字段,并且可能对如何报告它们有其他想法,所以我将这部分留给读者

欢迎任何对上述内容有改进的人提交PR

建议阅读:

  • 解析嵌套JSON的Internet参考。有很多
[
  { "pubMedId": "1234",
    "name": "Jay Sahn",
    "publications": [
      { "pubId": "abcd",
        "issn": "A1B2C3",
        "title": "Dynamic JSON Parsing: A Journey into Madness",
        "authors": [
          { "pubMedId": "1234" },
          { "pubMedId": "2345" }
        ]
      },
      { "pubId": "efgh",
        ...
      },
      ...
    ],
    ...
  },
  ...
]
function foo() {
  const sheet = /* get a sheet reference somehow */;
  const resp = UrlFetchApp.fetch(...).getContentText();
  const data = JSON.parse(resp);
  // paths relative to the outermost field, which for the imaginary source is an array of "author" objects
  const fields = ['pubMedId', 'name', 'publications/pubId', 'publications/title', 'publications/authors/pubMedId'];

  const output = data.map(function (author) {
    var row = fields.map(function (f) {
      var desiredField = f.split('/').reduce(delve_, author);
      return JSON.stringify(desiredField);
    });
    return row;
  });
  sheet.getRange(1, 1, output.length, output[0].length).setValues(output);
}

function delve_(parentObj, property, i, fullPath) {
  // Dive into the given object to get the path. If the parent is an array, access its elements.
  if (parentObj === undefined)
    return;

  // Simple case: parentObj is an Object, and property exists.
  const child = parentObj[property];
  if (child)
    return child;

  // Not a direct property / index, so perhaps a property on an object in an Array.
  if (parentObj.constructor === Array)
    return collate_(parentObj, fullPath.splice(i));

  console.warn({message: "Unhandled case / missing property", 
                args: {parent: parentObj, prop: property, index: i, pathArray: fullPath}});
  return; // property didn't exist, user error.
}
function collate_(arr, fields) {
  // Obtain the given property from all elements of the array.
  const results = arr.map(function (element) {
    return fields.slice().reduce(delve_, element);
  });
  return results;
}