【发布时间】: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