我正在尝试使用Google脚本在工作表上创建一个按钮,该按钮将创建另一个过滤工作表。然后,我需要单元格A2来填充我创建的已过滤IMPORTRANGE函数。问题是,该函数返回错误消息“在FILTER评估中未找到匹配项”。它说是因为IMPORTRANGE函数返回参考错误,因为尚未批准对原始电子表格的访问。这需要我手动剥离FILTER元素的功能,批准两张纸之间的访问,然后将FILTER元素重新添加到功能中。我想知道是否有某种方法可以自动批准工作表之间的访问,以便在第一次打开新创建的工作表时,一切都能按预期工作。
我创建的Google表格功能为=filter(IMPORTRANGE("https://docs.google.com/spreadsheets/d/103XMy_5qboN7sRcVbkJ0XAG7soNJdwURCv7cXJ4pS8E/edit#gid=0","Situations!A:Q"),(IMPORTRANGE("https://docs.google.com/spreadsheets/d/103XMy_5qboN7sRcVbkJ0XAG7soNJdwURCv7cXJ4pS8E/edit#gid=0","Situations!c:c")="9")+(IMPORTRANGE("https://docs.google.com/spreadsheets/d/103XMy_5qboN7sRcVbkJ0XAG7soNJdwURCv7cXJ4pS8E/edit#gid=0","Situations!g:g")="9"))
该函数将原始电子表格中的数据过滤到仅包含9
或C
列中的G
的行中
工作表包含需要共享的数据,因此必须对其进行过滤,因为只允许某些人看到工作表中的某些数据。我正在创建的按钮旨在自动为他们创建一个过滤的工作表,该工作表会在主工作表更新时更新。这是我到目前为止编写的代码:
function createFilteredSheet(e) {
SpreadsheetApp.getUi()
.createMenu('Create Filtered Sheet')
.addItem('Create Filtered Sheet', 'createFilteredSheet_')
.addToUi();
}
function createFilteredSheet_() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SituationsSheet = ss.getSheetByName('Situations');
var sheetId = ss.getId();
var labelArray = SituationsSheet.getRange('A1:Q1').getValues();
var filterValue = Browser.inputBox('Create Filtered Sheet', 'Enter the value of what you would like to filter:', Browser.Buttons.OK_CANCEL);
Logger.log(filterValue);
if(filterValue == 'cancel') {return;}
var shareEmail = [Browser.inputBox("Email Address to Share With", "Enter the email address you would like to share the sheet with:", Browser.Buttons.OK_CANCEL)];
Logger.log(shareEmail);
if(shareEmail == 'cancel') {return;}
var createNewSheet = SpreadsheetApp.create(filterValue + ' Filter Sheet')
.addEditors(shareEmail)
.getId();
var addFormula = SpreadsheetApp.openById(createNewSheet)
.getRange('A2')
.setValue('=filter(IMPORTRANGE("https://docs.google.com/spreadsheets/d/'+sheetId+'/edit#gid=0","Situations!A:Q"),(IMPORTRANGE("https://docs.google.com/spreadsheets/d/'+sheetId+'/edit#gid=0","Situations!c:c")="'+filterValue+'")+(IMPORTRANGE("https://docs.google.com/spreadsheets/d/'+sheetId+'/edit#gid=0","Situations!g:g")="'+filterValue+'"))');
var addLabels = SpreadsheetApp.openById(createNewSheet)
.getRange('A1:Q1')
.setValues(labelArray);
}