I believe your goal is as follows.
- You have 2 Google Spreadsheets "A" and "B".
- You want to make the user retrieve values from Spreadsheet "B" with the script of Spreadsheet "A".
- You don't want to share Spreadsheet "B" while Spreadsheet "A" is shared with the user.
In this case, as a workaround, how about accessing Spreadsheet "B" using Web Apps? By this, you can make the user access Spreadsheet "B" without sharing the Spreadsheet with the user. When this is reflected in your script, it becomes as follows.
Flow:
1. For Spreadsheet "B":
Please copy and paste the following script to the script editor of Spreadsheet "B".
function doGet(e) {
var SN = e.parameter.sn; // You can use the value of SN here.
var ss = SpreadsheetApp.getActiveSpreadsheet(); // Your 2nd Spreadsheet.
var sheets = ss.getSheets();
// search for data in ss sheets . . .
// return array of found data
var returnValues = ["sample"]; // Please replace this value for your actual script.
return ContentService.createTextOutput(JSON.stringify(returnValues));
}
2. Deploy Web Apps.
Please run this flow on the script editor of Spreadsheet "B". The detailed information can be seen at the official document.
- On the script editor, at the top right of the script editor, please click "click Deploy" -> "New deployment".
- Please click "Select type" -> "Web App".
- Please input the information about the Web App in the fields under "Deployment configuration".
- Please select "Me" for "Execute as".
- This is the importance of this workaround.
- Please select "Anyone" for "Who has access".
- In this case, the user is not required to use the access token. So please use this as a test case.
- Of course, you can also access your Web Apps using the access token. Please check this report.
- Please click "Deploy" button.
- Copy the URL of the Web App. It's like
https://script.google.com/macros/s/###/exec
.
3. For Spreadsheet "A":
Please copy and paste the following script to the script editor of Spreadsheet "A". And, set your Web Apps URL. In this case, please give the value to SN
.
function onSearch(SN) {
var url = "https://script.google.com/macros/s/###/exec"; // Please set your Web Apps URL here.
var res = UrlFetchApp.fetch(`${url}?sn=${SN}`);
var ar = JSON.parse(res.getContentText()); // This is the returned value from Spreadsheet "B".
// do something.
}
4. Testing:
When you run the script of onSearch
, the value of SN
is sent to Spreadsheet "B" and run the script of Spreadsheet "B", and the result values are returned. By this flow, you can retrieve the values from Spreadsheet "B" without sharing Spreadsheet "B" with the user.
Note:
- In this sample script, when you directly run
onSearch
, the value of SN
is not declared. So please be careful about this.
- When you modified the Google Apps Script, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful this.
- You can see the detail of this in the report of "Redeploying Web Apps without Changing URL of Web Apps for new IDE".
- My proposed script is a simple script. So please modify it for your actual situation.
References: