【问题标题】:How to insert the Title Value from an ISBN in Sheets using Scripts, JSON and Books API如何使用脚本、JSON 和 Books API 在表格中插入 ISBN 的标题值
【发布时间】:2021-01-15 23:27:30
【问题描述】:

第一次在谷歌应用脚​​本中编码,所以我提前为我的无知道歉。

我在 Digital Inspiration 上看到了这个很棒的脚本,但我很难将它写到工作表上。

我创建了一个包含以下 3 列数据的 Google 表格: 标题|作者|isbn 对于 isbns,我在 C2 和 C3 中有 2 个值:9781471178412 和 9780669397666

在 Google App Scripts 中,我使用 Google Books API 来提取书籍的标题,但一直对如何将 Google API 中的“标题”值插入我的 isbn 的 Google Sheet 感到困惑。它记录了正确的标题,但是当我在函数“fillInTheBlanks”中调用它时,它说它是未定义的。

非常感谢您能提供给这个新手的任何指导!谢谢!

function getBookDetails(isbn) {

 var ISBN_COLUMN = 2;

 var dataRange = SpreadsheetApp.getActiveSpreadsheet()
    .getDataRange();
 var bookValues = dataRange.getValues();

 for(var row = 1; row < bookValues.length; row++){   
     var isbn = bookValues[row][ISBN_COLUMN];

    var url = "https://www.googleapis.com/books/v1/volumes?country=US&q=isbn:" + isbn;

    var response = UrlFetchApp.fetch(url);
    var results = JSON.parse(response);

    if (results.totalItems) {
      // There'll be only 1 book per ISBN
       var book = results.items[0];
       var title = (book["volumeInfo"]["title"]);
       var authors = (book["volumeInfo"]["authors"]);
  
       // For debugging
       Logger.log(book);
       Logger.log(title);
       Logger.log(authors);

     }
  }
}

function fillInTheBlanks(){
  var TITLE_COLUMN = 0;
  var AUTHOR_COLUMN = 1;
  var ISBN_COLUMN = 2;

var dataRange = SpreadsheetApp.getActiveSpreadsheet()
    .getDataRange();
var bookValues = dataRange.getValues();

for(var row = 1; row < bookValues.length; row++){   
    var isbn = bookValues[row][ISBN_COLUMN];
    var title1 = bookValues[row][TITLE_COLUMN];
    var author1 = bookValues[row][AUTHOR_COLUMN];

    if(isbn != "" && (title1 === "" || author1 === "") ){
      var runresults = getBookDetails(isbn);
      
     if(title1 === "" && runresults.title){
        bookValues[row][TITLE_COLUMN] = runresults.title; 
      
    }
  }
  
}
dataRange.setValues(bookValues);   
}

【问题讨论】:

    标签: json scripting undefined isbn


    【解决方案1】:

    想通了!!我确信我的代码不像大多数代码那样优雅,但我很高兴能够让它在我第一次尝试 JSON/App 脚本时工作。

    function onOpen() {
      var ui = SpreadsheetApp.getUi();
      ui.createMenu('Book-list')
        .addItem('GoogleDataPull', 'fillInTheBlanks')
        .addSeparator()
        .addItem('OpenLibraryDataPull', 'fillInTheBlanksOL')
        .addToUi();
    }
    
    function getBookDetails(isbn) {
       var url = "https://www.googleapis.com/books/v1/volumes?country=US&q=isbn:" + isbn;
            var response = UrlFetchApp.fetch(url);
          var bookData = JSON.parse(response);
          if (bookData.totalItems) {
          // There'll be only 1 book per ISBN
           var book = bookData.items[0];
           var title = (book["volumeInfo"]["title"]);
    
    
           // For debugging
           Logger.log(book);
           Logger.log(title);
           return title;
      }
    }
    function fillInTheBlanks(){
      var TITLE_COLUMN = 0;
      var ISBN_COLUMN = 1;
    
    
    
    
    
    var dataRange = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = dataRange.getSheets()[0];
    // This represents ALL the data
    var range = sheet.getDataRange();
    var bookValues = range.getValues();
    
    
     // var dataRange = SpreadsheetApp.getActiveSpreadsheet()
     //   .getDataRange();
     // var bookValues = dataRange.getValues();
      for(var row = 1; row < bookValues.length; row++){   
        var isbn = bookValues[row][ISBN_COLUMN];
        var title1 = bookValues[row][TITLE_COLUMN];
        
        if(isbn != "" && title1 === "" ){
         // var bookData = getBookDetails(isbn);
          
          
          if(title1 === "" && getBookDetails(isbn)){
            bookValues[row][TITLE_COLUMN] = getBookDetails(isbn); 
            range.setValues(bookValues); 
          
          }
      }
      
    }
    //dataRange.setValues(bookValues);   
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-31
      • 1970-01-01
      • 2018-12-18
      • 1970-01-01
      • 2019-06-06
      • 2017-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多