with IMPORTRANGE SpreadSheets A, B, C, etc Requests acces to pull data from SpreadSheet 1 (pic 1). Exemple:
=IMPORTRANGE("SpreadSheet 1"; "Sheet!A1")
I want to remove acess ( revoke permission) from Spreadsheet A to pull data from Spreadsheet 1 (pic 2) undo that allow acess on the pic 1, to revoke permission to Spreadsheet A to pull data from Spreadsheet 1
It is just an example to simplify, because in reality I have over 200 Spreadsheets connecting to 1 (actually 2) Database Spreadsheets, that is why I want to revoke permissions, there is a limit on how many Spreadsheets can access one Spreadsheet and I have reached that limit many times already
Picture of real Spreadsheet to help understand part of my code, script will run on same Spreadsheet that requests access
i may be wrong but an old script i had, i think used to work
function removePermission()
{
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ss_c = ss.getSheetByName('Config');
var currentSheetId = ss.getId();
var targetId = "";
if (ss_c.getRange("B6").getValue() == 1)
{
targetId = ss_c.getRange("D6").getValue();
}
else if (ss_c.getRange("B6").getValue() == 2)
{
targetId = ss_c.getRange("D7").getValue();
}
var currentFile = DriveApp.getFileById(currentSheetId);
var targetFile = DriveApp.getFileById(targetId);
var targetPermissions = targetFile.getPermissions();
for (var i = 0; i < targetPermissions.length; i++)
{
var permission = targetPermissions[i];
if (permission.getType() == "user" && permission.hasAccess())
{
currentFile.removeEditor(permission.getEmail()); // or the next one
// targetFile.removeEditor(permission.getEmail());
}
}
}
but now it displays error TypeError: targetFile.getPermissions is not a function
so i tried to modify with what i searched
function test()
{
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ss_c = ss.getSheetByName('Config');
var currentSheetId = ss.getId();
var targetId = "";
if (ss_c.getRange("B6").getValue() == 1)
{
targetId = ss_c.getRange("D6").getValue();
}
else if (ss_c.getRange("B6").getValue() == 2)
{
targetId = ss_c.getRange("D7").getValue();
}
var currentFile = DriveApp.getFileById(currentSheetId);
var targetFile = DriveApp.getFileById(targetId);
var targetEditors = targetFile.getEditors();
var currentEditors = currentFile.getEditors();
var activeUserEmail = Session.getActiveUser().getEmail();
for (var i = 0; i < targetEditors.length; i++) {
var editor = targetEditors[i];
var editorEmail = editor.getEmail();
// Check if the editor exists in the current file before removing
if (editorEmail !== activeUserEmail && currentEditors.some(e => e.getEmail() === editorEmail)) {
targetFile.removeEditor(editorEmail);
}
}
}
passes, but doesn't work
im not familiar how editors work exactly in this case and searching i couldn't find related problems and solutions
im the owner and only user, not sure but it is related to files not users, help is appreciated, thanks