使用脚本替换 Google 表格超链接中的文本

问题描述 投票:0回答:0

我不是一个编码员,但我使用了我在这个网站和其他网站上找到的一些位来组合一个小的谷歌脚本,用于将文件组织到谷歌表格中。基本上,它从指定的 Drive 文件夹中提取文件名和链接,并将它们与相应的超链接一起打印到 Google 表格中,该脚本对此非常有效。我想从超链接中删除部分文件名,但不太明白。这是脚本到目前为止的内容,以及我希望编辑的超链接显示的内容:

文件编号 文件链接 超级链接 已编辑的超链接
亚当·斯密 - 文件 2023-1 https://drive.google.com/file/d/1mh82jf89j23/view?uspdrivesdk =超链接(“https://drive.google.com/file/d/1mh82jf89j23/view?uspdrivesdk”,“亚当·斯密 - 文件 2023-1”) =超链接(“https://drive.google.com/file/d/1mh82jf89j23/view?uspdrivesdk”,“2023-1”)
约翰梅纳德 - 文件 2023-1 https://drive.google.com/file/d/ijeficoq38vc8/view?uspdrivesdk =超链接(“https://drive.google.com/file/d/ijeficoq38vc8/view?uspdrivesdk”,“John Maynard - Document 2023-1”) =超链接(“https://drive.google.com/file/d/ijeficoq38vc8/view?uspdrivesdk”,“2023-1”)

我知道如何使用 regexreplace 和 substitute 作为 Google 表格公式,但它不适用于超链接,而且我不知道如何将它们合并到脚本中,如下所示:

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getActiveSheet();
  var c1 = s.getRange("C1")
  var c2 = s.getRange("B1");
  var c3 = s.getRange("A1")
  var fldr = DriveApp.getFolderById("1Kt43CdFyWBFrx1Wo4eJMLf0UB0RTPYiS");
  var files = fldr.getFiles();
  var urls = [], names = [], ids = [], f, hyp, str;
  while (files.hasNext()) {
    f = files.next();
    hyp = f.getUrl();
    str = '=hyperlink("' + f.getUrl() + '", "'+ f.getName() + '")';
    urls.push([str]);
    names.push([f.getUrl()]);
    ids.push([f.getName().replace(/\.[^/.]+$/, "")]); //Remove filename extensions
    //ids.push([f.getName()]); //Filename with extensions
  }

  s.getRange(c1.getRow(), c1.getColumn(), urls.length).setFormulas(urls);
  s.getRange(c2.getRow(), c2.getColumn(), names.length).setValues(names);
  s.getRange(c3.getRow(), c3.getColumn(), ids.length).setValues(ids);


}

感谢任何帮助-谢谢!

google-apps-script google-sheets google-apps
© www.soinside.com 2019 - 2024. All rights reserved.