【问题标题】:Look Thru all my spreadsheets for a sheet with a specific name app script通过我所有的电子表格查找具有特定名称应用脚本的工作表
【发布时间】:2021-12-29 12:15:32
【问题描述】:

嗨。 我正在尝试创建一个脚本,该脚本将查看驱动器中的所有电子表格并搜索具有特定名称的工作表并返回工作表 URL / ID 和电子表格名称

这是我目前得到的,但它真的很慢而且效率不高 因为我在驱动器中有大约 190 个 Google 表格,并且我正在寻找的每个表格名称在 190 个中的大约 20 个中都有一个表格

我认为如果有办法通过在驱动器中搜索工作表名称来做到这一点,那么它会缩小范围 从我所有的谷歌表格到仅包含表格名称值的谷歌表格,它可能会使其更快,但我不确定如何执行此操作,我认为它对我来说不够快

非常感谢您在专业方面帮助我


function getSheetNames(text = "Sheet Name To Search for") {
  var arraywithfoundsheets = [];
  var sp = SpreadsheetApp;
  // Get all Sheets in drive
  var allsheets = DriveApp.getFilesByType(MimeType.GOOGLE_SHEETS);
  //loop thru all spredsheets
  while (allsheets.hasNext()) {
    var spreadsheet = sp
      .open(allsheets.next());

    // return an array with all the sheet names from the spreadsheet
    var allsheetsinspreadsheet = spreadsheet.getSheets()
      .map(su => su.getName())

    // if this spredsheet contains a sheet with the specified name
    if (allsheetsinspreadsheet.indexOf(text) > -1) {

      // add the sheet with the specified name to the array
      arraywithfoundsheets.push([spreadsheet.getUrl() 
                                 + '#gid=' 
                                 + spreadsheet.
      getSheetByName(allsheetsinspreadsheet[allsheetsinspreadsheet.indexOf(text)])
                                    .getSheetId(),spreadsheet.getName()]);
    }
  }
Logger.log(arraywithfoundsheets);
return arraywithfoundsheets;
}

【问题讨论】:

  • 您是所有相关工作表的所有者吗?将工作表限制为您拥有的工作表可能会有所帮助...
  • 不,我需要拥有和共享的表格
  • 只是出于好奇,这是一个奇怪的请求。为什么有人要反复在电子表格中搜索工作表中的特定标签名称?
  • 因为我有很多电子表格,每个月都有工作表,我希望能够搜索特定月份并在我的任何电子表格中获取该月所有工作表的链接

标签: google-apps-script google-sheets google-drive-api


【解决方案1】:

可能这会快一点(如果它可以工作,我还没有尝试过):

function getSheetNames(text = "Sheet Name To Search for") {
  var arraywithfoundsheets = [];
  var sp = SpreadsheetApp;

  // Get all Sheets in drive
  var allsheets = DriveApp.getFilesByType(MimeType.GOOGLE_SHEETS);

  //loop thru all spredsheets
  while (allsheets.hasNext()) {
    var spreadsheet = sp.open(allsheets.next());
    var sheet = spreadsheet.getByName(text);   // no need to find for the sheet if you have its name
    if (!sheet) continue;                      // skip if there is no sheet with such name

    arraywithfoundsheets.push([
      spreadsheet.getUrl() + '#gid=' + sheet.getSheetId(), // if you have the sheet already you can get its ID directly
      spreadsheet.getName()
    ]);

  }

  Logger.log(arraywithfoundsheets);
  return arraywithfoundsheets;
}

它对服务器的调用更少。但实际上我认为没有办法让脚本更快地引人注目。

【讨论】:

    【解决方案2】:

    这确实可以运行,但打开每个电子表格需要很长时间:

    function getSheetUrlId(name = 'Sheet4') {
      const ss = SpreadsheetApp.getActive()
      const fit = DriveApp.getFilesByType(MimeType.GOOGLE_SHEETS);
      let shts = [];
      while (fit.hasNext()) {
        let ss = SpreadsheetApp.openById(fit.next().getId());
        if (ss) {
          let sh = ss.getSheetByName(name);
          if (sh) {
            let ssurl = ss.getUrl();
            let shid = sh.getSheetId();
            let url = `${ssurl}#gid=${shid}`
            shts.push({ url: url, id: sh.getSheetId(), name: sh.getName() });
          }
        }
      }
      Logger.log(JSON.stringify(shts));
    }
    

    我怀疑使用 Sheets API 的解决方案可能会提供更快的解决方案。有点我没兴趣自己去发现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多