如何使用谷歌脚本在谷歌电子表格上显示包含搜索关键字的网站



目前,我拥有的Google脚本代码创建了一个按钮,提示用户输入要搜索的关键字。然后,它应该通过检索其URL/名称/标题并将其显示在Google表格上各自的列中,从而在电子表格上返回结果。我希望能够进行搜索,搜索托管指向网站子页面链接的特定网站,并在该站点上的任何位置返回包含搜索词的网站,并将结果粘贴到电子表格中。编辑我在运行代码时遇到的问题是,搜索关键字后,电子表格返回一个对话框,指示"找不到您请求的资源",但我正在搜索的网站实际上在多个页面上都有关键字。 这是我需要帮助的代码:

function websearch() {
// Prompt the user for a search term
var websearchTerm = Browser.inputBox("Enter the keyword to search for:");
// Get the active spreadsheet and the active sheet
var wss = SpreadsheetApp.getActiveSpreadsheet();
var wsheet = wss.getActiveSheet();
// Set up the spreadsheet to display the results
var theaders = [["Name", "Title", "Web Address"]];
wsheet.clear();
wsheet.getRange("A1:C1").setValues(theaders);
// Search the webpages associated with the given URL 
var site = SitesApp.getSite("https://sites.google.com/a/umich.edu/hb-ua/");
//var matches = site.search(websearchTerm);
var matches = site.search("fullText contains '"+websearchTerm.replace("'","'")+"'");
var woutput = [];
// Loop through the results and get the file name, file title, and URL
while (site.hasNext()) {
var wfile = site.next();
var wname = wfile.getName();
var wtitle = wfile.getTitle();
var wurl = wfile.getUrl();
// push the file details to our output array (essentially pushing a row of data)
woutput.push([wname, wtitle, wurl]);
}
// write data to the sheet
wsheet.getRange(2, 1, woutput.length, 3).setValues(woutput);
}

然后我知道这段代码肯定有效,它完成了我请求帮助的事情,但通过谷歌驱动器而不是谷歌网站/特定的父网站进行搜索:

function search(Phrase, FolderID) {
// Prompt the user for a search term
var searchTerm = Browser.inputBox("Enter the string to search for:");
// Get the active spreadsheet and the active sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// Set up the spreadsheet to display the results
var headers = [["File Name", "File Type", "URL"]];
sheet.clear();
sheet.getRange("A1:C1").setValues(headers);
// Search the files in the user's Google Drive for the search term based on if the word is included in thefile or name
// Search Reference Guide: https://developers.google.com/apps-script/reference/drive/drive-app#searchFiles(String)
var files = DriveApp.searchFiles("fullText contains '"+searchTerm.replace("'","'")+"'");
//var SearchString = 'fullText contains "' + Phrase + '" and "' + FolderID + '" in parents';
//var files = DriveApp.searchFiles(SearchString);
// create an array to store our data to be written to the sheet 
var output = [];
// Loop through the results and get the file name, file type, and URL
while (files.hasNext()) {
var file = files.next();
var name = file.getName();
var type = file.getMimeType();
var url = file.getUrl();
// push the file details to our output array (essentially pushing a row of data)
output.push([name, type, url]);
}
// write data to the sheet
sheet.getRange(2, 1, output.length, 3).setValues(output);
}

这是搜索对话框提示代码:

function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var searchMenuEntries = [ {name: "Search in all files", functionName: "search"}];
var websearchMenuEntries = [ {name: "Search in all files", functionName: "websearch"}];
ss.addMenu("Search Google Drive", searchMenuEntries);
ss.addMenu("Search UMich", websearchMenuEntries);
}

你没有描述什么是错误的,或者显示什么错误。

但是,从云端硬盘脚本转换而来的类比脚本看起来不错,除了一件事:

  • 方法DriveApp.searchFiles()返回文件迭代器,但
  • 方法Site.search()只返回页面数组。

尝试使用for()foreach().forEach(),而不是.next()迭代。

最新更新