有人可以帮助我编写脚本吗?我已尝试了所有方法,APP 脚本执行日志显示: 执行开始于 7:35:32PM 通知执行已完成。没有错误。但它不会创建电子表格。它只执行了一次,并且缺少几列,所以我回去检查代码,它再也没有工作过。
function myFunction() {
function createNewGoogleSheet() {
try {
var newSheet = SpreadsheetApp.create('New Spreadsheet');
Logger.log('New Google Sheet created with ID: ' + newSheet.getId());
} catch (error) {
Logger.log('Error creating new Google Sheet: ' + error.message);
throw error; // This will help to terminate the script and log the error in the execution log.
}
}
function job_processGoogleSheetData() {
Logger.log('job_processGoogleSheetData function is starting...');
// ... (existing code)
// run Function to create Google Folders
var doIsbnLookup = isbnLookup_(spreadsheetData);
// check success status
if (doIsbnLookup) {
// display Toast notification
Logger.log('Finished Successfully');
// Create a new Google Sheet
createNewGoogleSheet();
} else {
// script completed with error
// display Toast notification
Logger.log('With errors. Please see Logs', 'Finished');
}
// Log ending of the script
Logger.log('Script finished');
}
// sheet tab name containing the data
const DATA_TAB_NAME = 'Form Responses 1';
// Set sheet column order (using 0 index). IE: COLUMN A is 0, COLUMN B is 1
const COLUMN_NUMBER_ISBN = 2;
const COLUMN_NUMBER_TITLE = 5;
// Set sheet column order for updating from the API details that come back (starting with Column A = 1, Column B = 2)
const COLUMN_NUMBER_AUTHORS = 7;
const COLUMN_NUMBER_DESCRIPTION = 8;
// API KEY
const API_KEY = "AIzaSyD-YDmn88Cv1ZIGkyMj1jQWrhhbIhm7ItQ";
const API_URL = "https://www.googleapis.com/books/v1/volumes?country=US";
/**
*
* DO NOT CHANGE ANYTHING UNDER THIS LINE
*
* ONLY CHANGE THINGS IN THE CONFIG.GS FILE
*
*/
/**
* Do the lookup stuff
*/
function job_processGoogleSheetData() {
// get current spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Log starting of the script
Logger.log('Script has started');
// get TimeZone
var timeZone = ss.getSpreadsheetTimeZone();
// get Data sheet
var dataSheet = ss.getSheetByName(DATA_TAB_NAME);
// get all data as a 2-D array
var data = dataSheet.getDataRange().getValues();
// create a name:value pair array to send the data to the next Function
var spreadsheetData = {ss:ss, timeZone:timeZone, dataSheet:dataSheet, data:data};
// run Function to create Google Folders
var doIsbnLookup = isbnLookup_(spreadsheetData);
// check success status
if (doIsbnLookup) {
// display Toast notification
Logger.log('Finished Successfully');
}
else {
// script completed with error
// display Toast notification
Logger.log('With errors. Please see Logs', 'Finished');
}
// Log starting of the script
Logger.log('Script finished');
}
function isbnLookup_(spreadsheetData) {
// extract data from name:value pair array
var ss = spreadsheetData['ss'];
var timeZone = spreadsheetData['timeZone'];
var dataSheet = spreadsheetData['dataSheet'];
var data = spreadsheetData['data'];
// get last row number so we know when to end the loop
var lastRow = dataSheet.getLastRow();
var folderIdMap = new Object();
// start of loop to go through each row iteratively
for (var i=1; i<lastRow; i++) {
// extract values from row of data for easier reference below
var isbn = data[i][COLUMN_NUMBER_ISBN];
var title = data[i][COLUMN_NUMBER_TITLE];
// only perform this row if the title is blank
if(title == '') {
Logger.log('Looking Up ISBN: ' + isbn);
// run Function to get the book info from the API
var bookData = getBookDetails_(isbn);
// set the column number for the update method later
var title_column_number = COLUMN_NUMBER_TITLE + 1;
// check data came back correctly
if (bookData) {
// extract details into vars for easier reference later
var title = (bookData["volumeInfo"]["title"]);
var description = (bookData["volumeInfo"]["description"]);
var authors = (bookData["volumeInfo"]["authors"]);
// set values in apropriate columns
dataSheet.getRange(i+1, title_column_number).setValue(title);
dataSheet.getRange(i+1, COLUMN_NUMBER_AUTHORS).setValue(authors);
dataSheet.getRange(i+1, COLUMN_NUMBER_DESCRIPTION).setValue(description);
// write all pending updates to the google sheet using flush() method
SpreadsheetApp.flush();
} else {
// write error into Title cell and return false value
dataSheet.getRange(i+1, title_column_number).setValue('Error finding ISBN data. Please see Logs');
return false;
}
} else {
Logger.log('Skipping Row - ISBN Data already set - Parsing Next Row');
}
} // end of loop to go through each row in turn **********************************
// completed successfully
return true;
}
function getBookDetails_(isbn) {
// Query the book database by ISBN code.
var url = API_URL + "&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];
return book;
}
return false;
}
}
它应该做的是创建一个包含列、标题、描述、作者、印刷类型、出版商等的电子表格,然后我就可以通过输入 UPC 或 ISBN 号来填充图书元数据。一旦我让代码工作起来,我将添加脚本来制作它,这样我就可以导入 ISBN 和 UPC 以在以后需要时填充电子表格,并添加脚本以使其与 Scandit 应用程序兼容,但我从未做到这一点。
这对我有用。我运行了两次,它创建了两个电子表格。
function createNewGoogleSheet() {
try {
var newSheet = SpreadsheetApp.create('New Spreadsheet');
}
catch (error) {
Logger.log('Error creating new Google Sheet: ' + error.message);
}
}