I have a custom function built for our Google Sheet 1. Then, using IMPORTRANGE, a number is imported from Google Sheet 1 to Google Sheet 2. The number in Google Sheet 1 is calculated using the custom function mentioned before.
The issue is that, if Google Sheet 1 is not opened manually, the IMPORTRANGE function in Google Sheet 2 will show up as #NAME, and only fix itself once a user enters Google Sheet 1.
Is there any way to fix this issue, without having a real person enter Google Sheet 1?
Related
I guess this question is the most similar to my current question. But basically, I have a google sheets file that has several sheets along the bottom. I would like for the user to execute this script on whatever sheet they have open at the time.
And like the previous question said according to this documentation, the active sheet is the one that is being displayed in the spreadsheet UI. I assume this means if I have the google sheets file open in another tab, that the sheet currently selected and viewable by me is intended to be the active sheet.
However, the actual active sheet returned is always the leftmost sheet along the bottom tabs. I've confirmed it by switching the order of the sheets. So no matter what sheet I actually have open and visible in the UI, it always gets the leftmost sheet. Here is the line that gets it.
var Sheet = SpreadsheetApp.openById("redactedid").getActiveSheet()
Is this a known error with App Script? Or is the documentation wrong? If it is, is there a workaround for getting the currently open sheet in the UI? I don't want anyone to have to hardcode a sheetname in the code as the file is constantly changing and sheets are being added.
Notes:
To use getActiveSheet() or any active object, the calling
Script must be bound to the Spreadsheet
Script function should be invoked from menu or button or macro keyboard shortcuts and NOT from webapp or API( Script editor also works, but not preferred).
Spreadsheet must be open/visible in the user interface/browser preferably in the active/currently selected window.
All previous calls to get objects must have used active. For eg, To get active range, You should have first got active spreadsheet => active sheet => active range(the chain of active objects). If any of the calls is not to the active object(say if you used getSheetByName() instead of getActiveSheet() in the middle step), the final call is likely to default to the default object(First sheet A1).
Solution:
Here, To get Spreadsheet, use SpreadsheetApp.getActive() instead of SpreadsheetApp.openById(), So that getActiveSheet() will be under the chain of active objects.
Snippet:
const sheet = SpreadsheetApp.getActive().getActiveSheet();
It's known that the getActiveSheet method returns the first sheet when used with an spreadsheet that is not the active spreadsheet.
The active spreadsheet is retrieved by using getActiveSpreadsheet. I only works on scripts bounded to an spreadsheet and in G Suite Editors add-on for spreadsheets.
In the the same way, getCurrentCell and getActiveRange returns A1 when corresponding sheet is not an active sheet (meaning it's not a sheet from an active spreadsheet)
Resources
https://developers.google.com/apps-script/guides/sheets
Related
Google Apps Script, select a sheet - Sets values in first sheet tab - not a specific sheet tab
All the points in the accepted solution are all true, but sometimes your spreadsheet looses some connection to something on Google's back end or the cache gets corrupted, who knows. The fix is to close the all tabs for the worksheet, editor, triggers, executions -- whichever ones you have open for that spreadsheet, close them. Reopen the spreadsheet. Start again. It's fine now.
I guess this question is the most similar to my current question. But basically, I have a google sheets file that has several sheets along the bottom. I would like for the user to execute this script on whatever sheet they have open at the time.
And like the previous question said according to this documentation, the active sheet is the one that is being displayed in the spreadsheet UI. I assume this means if I have the google sheets file open in another tab, that the sheet currently selected and viewable by me is intended to be the active sheet.
However, the actual active sheet returned is always the leftmost sheet along the bottom tabs. I've confirmed it by switching the order of the sheets. So no matter what sheet I actually have open and visible in the UI, it always gets the leftmost sheet. Here is the line that gets it.
var Sheet = SpreadsheetApp.openById("redactedid").getActiveSheet()
Is this a known error with App Script? Or is the documentation wrong? If it is, is there a workaround for getting the currently open sheet in the UI? I don't want anyone to have to hardcode a sheetname in the code as the file is constantly changing and sheets are being added.
Notes:
To use getActiveSheet() or any active object, the calling
Script must be bound to the Spreadsheet
Script function should be invoked from menu or button or macro keyboard shortcuts and NOT from webapp or API( Script editor also works, but not preferred).
Spreadsheet must be open/visible in the user interface/browser preferably in the active/currently selected window.
All previous calls to get objects must have used active. For eg, To get active range, You should have first got active spreadsheet => active sheet => active range(the chain of active objects). If any of the calls is not to the active object(say if you used getSheetByName() instead of getActiveSheet() in the middle step), the final call is likely to default to the default object(First sheet A1).
Solution:
Here, To get Spreadsheet, use SpreadsheetApp.getActive() instead of SpreadsheetApp.openById(), So that getActiveSheet() will be under the chain of active objects.
Snippet:
const sheet = SpreadsheetApp.getActive().getActiveSheet();
It's known that the getActiveSheet method returns the first sheet when used with an spreadsheet that is not the active spreadsheet.
The active spreadsheet is retrieved by using getActiveSpreadsheet. I only works on scripts bounded to an spreadsheet and in G Suite Editors add-on for spreadsheets.
In the the same way, getCurrentCell and getActiveRange returns A1 when corresponding sheet is not an active sheet (meaning it's not a sheet from an active spreadsheet)
Resources
https://developers.google.com/apps-script/guides/sheets
Related
Google Apps Script, select a sheet - Sets values in first sheet tab - not a specific sheet tab
All the points in the accepted solution are all true, but sometimes your spreadsheet looses some connection to something on Google's back end or the cache gets corrupted, who knows. The fix is to close the all tabs for the worksheet, editor, triggers, executions -- whichever ones you have open for that spreadsheet, close them. Reopen the spreadsheet. Start again. It's fine now.
I have an active spreadsheet, I want to select a cell in this spreadsheet and then bring me to another Spreadsheet. I used app.openByUrl to open the spreadsheet and then get my wanted sheet by name and then activate the cell by getRange, the script is finished but it didn't take me from the active spreadsheet to the other spreadsheet, the cursor remains in the cell of the active Spreadsheet. I use the following code. Does anyone has an idea?
app.openByUrl("https://docs.google.com/spreadsheets/d/1S9Kc6foLZ_bXqs4/edit#gid=502179704").getSheetByName("Verification Candidates").getRange('D4:F4').activate();
It's not possible to activate an external spreadsheet by solely using Google Apps Script as
Class Spreadsheet doesn't include like similar to Class Range activate
The Class Spreadsheet "open" methods open the spreadsheet on the server side, not on client side.
You might be able to do what you are looking by using other tools (i.e. a complex bookmarklet)
I've been trying to build a script which I can use to switch from a given sheet in one spreadsheet, to a given sheet in another spreadsheet, in the same browser tab, where both spreadsheets are on the same google drive and in the same folder.
The purpose for doing this is I have more data than I can fit on a single google sheets spreadsheet (More than 200 individual sheets are needed) and I'd like to be able to navigate between them as seamlessly as possible.
I could have sworn I had a simple script like this working at some point, but I'm not sure. The script does appear to run, but it there is no result, it doesn't actually open the other sheet.
I've tried using both .openByID and .openByUrl, and both appear to give me the same result.
I set up two new spreadsheets, A and B, each with their own script, A pointing to B, and B pointing to A. The destination sheet within the spreadsheet has been named MainA and MainB.
//This is your "Go Home" Button
//URL of A: https://docs.google.com/spreadsheets /d/1LzsxZ3cCcOltELM_0T4VwipqYv5BbBDj9ugAZcciNHQ/edit#gid=0
//URL of B: https://docs.google.com/spreadsheets/d/1wrjr9VSsHb63RKz87JQbe20mCo1CteKHtsAiUGrb6O0/edit#gid=0
//ID of A: 1LzsxZ3cCcOltELM_0T4VwipqYv5BbBDj9ugAZcciNHQ
//ID of B: 1wrjr9VSsHb63RKz87JQbe20mCo1CteKHtsAiUGrb6O0
function goto_a() {
var ss = SpreadsheetApp.openById("1LzsxZ3cCcOltELM_0T4VwipqYv5BbBDj9ugAZcciNHQ")
ss.setActiveSheet(ss.getSheetByName("MainA"));
}
Obv the script for A is similar to this one, but with differently named function, destination sheet, and spreadsheet ID
In both cases, I'm using a button (drawing image) with the relevant script attached. If need be, I can share these spreadsheets so that the script can be tested.
Any advice would be great, thanks in advance.
As written in the official documentation ,
//Note that the spreadsheet is NOT physically opened on the client side.
// It is opened on the server only (for modification by the script).
You can alternatively provide a link using Browser.msgBox() for the user to click.
Related Question
I have a Google Script that reads from a Google Sheet. The only content of the Google Sheet is an importData() function that imports a CSV file. Will the importData() function in the Google Sheet be called every time the Google Script accesses the data in the Google Sheet? Or does the importData() function only get called when the Google Sheet is opened manually?
importData() is a formula, that is evaluated and reevaulated when a user opens the spreadsheet and when other cells that are referenced by the formula change.
So, this means the output of the formula is not available for Apps Scripts.
However, using Apps Script you can easily write the code to do what the formula does.
See this question for how to parse CSV data