如何使用Google Scripts在Google电子表格上显示包含搜索关键字的网站

时间:2018-05-29 13:22:44

标签: google-apps-script

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

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

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

difflib.SequenceMatcher

1 个答案:

答案 0 :(得分:0)

您没有描述错误或显示的错误。

但是你从驱动程序脚本转换的类比剧本看起来很不错,除了一件事:

尝试使用for()foreach().forEach()代替.next()进行迭代。