谷歌电子表格在多个单元格中显示脚本中的值

google spreadsheet display values from script in multiple cells

本文关键字:显示 脚本 单元格 电子表格 谷歌      更新时间:2023-12-12

第一次尝试谷歌应用程序脚本。我用谷歌电子表格中的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"]);
 }
}

这就是您想要的吗?

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);
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);  
 }
}