【问题标题】:google spreadsheet display values from script in multiple cells谷歌电子表格在多个单元格中显示脚本中的值
【发布时间】:2016-07-22 14:33:06
【问题描述】:

第一次尝试谷歌应用脚​​本。我在谷歌电子表格中使用 api 键设置了这个脚本,并将其添加到脚本中的 url 变量中。该脚本运行没有错误,但我不知道如何在单个单元格中显示特定变量。我查看了文档,但还没有遇到类似的示例。

如果我在 A 列中有 ISBN,我会在 B 列中输入什么来显示标题?

function getBookDetails(isbn) {

// Query the book database by ISBN code.
isbn = isbn || "9781451648546"; // Steve Jobs book 

var url = "https://www.googleapis.com/books/v1/volumes?key=myAPI&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 subtitle = (book["volumeInfo"]["subtitle"]);
var authors = (book["volumeInfo"]["authors"]);
var printType = (book["volumeInfo"]["printType"]);
var pageCount = (book["volumeInfo"]["pageCount"]);
var publisher = (book["volumeInfo"]["publisher"]);
var publishedDate = (book["volumeInfo"]["publishedDate"]);
var webReaderLink = (book["accessInfo"]["webReaderLink"]);
 }

}

【问题讨论】:

    标签: javascript google-apps-script google-sheets


    【解决方案1】:

    这就是你要找的吗?

    var resultRow = [[isbn,title,subtitle,authors,printType,pageCount,publisher,publishedDate,webReaderLink]];
    var sh = SpreadsheetApp.getActiveSheet();
    sh.getRange(sh.getLastRow()+1,1,resultRow.length,resultRow[0].length).setValues(resultRow);
    

    【讨论】:

    • 很棒的塞尔吉,这行得通!尽管接近尾声,但代码中有一个小错字。我将 steValues 更改为 setValues。
    • 虽然有一个问题,它在脚本中显示示例 isbn 编号,但是当我在 A1 中有一个 ISBN 编号并将 =getBookDetails(A1) 放入单元格 A2 时,我收到一个错误:你没有有权调用 setValues(第 32 行)。
    • 你不能从一个单元格中的自定义函数更改另一个单元格的内容(这可能是你想要做的......),你能显示你使用的确切代码
    • 所以我注意到 :) 我最终只使用了return resultRow;(请参阅下面的答案中的代码)。到目前为止,我的脚本并不是真正可用的,因为它为每个 isbn 进行了 api 调用。我已经达到了我的 api 调用限制。我必须想出另一种方法来加载列表,使用更少的 api 调用,或者最好只对新条目进行 api 调用。
    【解决方案2】:
    function getBookDetails(isbn) {
    
    // Query the book database by ISBN code.
    
    if (isbn !== "") { 
    
    var url = "https://www.googleapis.com/books/v1/volumes?key=AIzaSyAsTRXP0Eqy0Q4mQ1A00xQWYVzcgHyBKiM&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 subtitle = (book["volumeInfo"]["subtitle"]);
    var authors = (book["volumeInfo"]["authors"]);
    var printType = (book["volumeInfo"]["printType"]);
    var pageCount = (book["volumeInfo"]["pageCount"]);
    var publisher = (book["volumeInfo"]["publisher"]);
    var publishedDate = (book["volumeInfo"]["publishedDate"]);
    var webReaderLink = (book["accessInfo"]["webReaderLink"]);
    
    // For debugging
    Logger.log(book);
    
    }
    var resultRow = [[title,authors]];
    
    
    return resultRow;
    
    // var sh = SpreadsheetApp.getActiveSheet();
    // sh.getRange(sh.getLastRow()+1,1,resultRow.length,resultRow[0].length).setValues(resultRow);  
     }
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      相关资源
      最近更新 更多