Hyperlink to a specific sheet - google-sheets

I would like to open a specific sheet of a Google Sheets from a hyperlink in another spreadsheet.
I have different links in my master spreadsheet and each should have a hyperlink to the same slave spreadsheet but to a different sheet.
I know hyperlink function but it doesn't go to a specific sheet.

You can use this custom script (Tools > Script Editor) function and connect it with e.g. custom drawing (Insert > Drawing... > Save and Close, then right click on new drawing> Assign Script... > "goToSheet2")
function goToSheet2() {
goToSheet("Sheet2");
}
function goToSheet(sheetName) {
var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
SpreadsheetApp.setActiveSheet(sheet);
}
Update:
In the newest version you can select cell and add link (Insert > Link) and select link to specific sheet directly:

The HYPERLINK function can link to another sheet in the same workbook; if you observe the URL of the spreadsheet, at the end of it there is #gid=x where x is unique for each sheet.
The problem is, it will open the sheet as a new instance of the spreadsheet in another tab, which is probably not desirable. The workaround would be to insert images or drawings as buttons, and assigning a script to them that will activate specific sheets.

I personnaly did this based on what #rejthy said:
In scripts I created this function:
/**
* Return the id of the sheet. (by name)
*
* #return The ID of the sheet
* #customfunction
*/
function GET_SHEET_ID(sheetName) {
var sheetId = SpreadsheetApp.getActive().getSheetByName(sheetName).getSheetId();
return sheetId;
}
and then in my sheet where I need the link I did this: =HYPERLINK("#gid="&GET_SHEET_ID("Factures - "&$B$1);"Année en cours")

So what I understand from the OP is that you have one master spreadsheet that you want to have links to individual sheets, where one or more of those sheets may be in single or multiple spreadsheet files.
The HYPERLINK function only turns a URL into a hyperlink and is really only useful when you want to have hypertext instead of just a link. If you enter the raw URL as the data, it's automatically turned into a hyperlink, so there's no additional work.
As mentioned in other answers, the solution is to have the spreadsheet's URL then use the gid value to calculate the link to the desired sheet within the spreadsheet. You can write a simple app that collects all of the individual sheets' links and writes them into the master.
Below are some snippets of pseudocode (Python) that can help you get started. I'm leaving out all the boilerplate auth code, but if you need it, see this blog post and this video. The code below assumes your API service endpoint is SHEETS.
This reads a target spreadsheet to build links for each of its sheets:
# open target Sheet, get all sheets & Sheet URL
SHEET_ID = TARGET_SHEET_DRIVE_FILE_ID
res = SHEETS.spreadsheets().get(spreadsheetId=SHEET_ID,
fields='sheets,spreadsheetUrl').execute()
sheets = res.get('sheets', [])
url = res['spreadsheetUrl']
# for each sheet, dump out its name & full URL
for sheet in sheets:
data = sheet['properties']
print('** Sheet title: %r' % data['title'])
print(' - Link: %s#gid=%s' % (url, data['sheetId']))
Instead of printing to the screen, let's say you stored them in a (name, URL) 2-tuple array in your app, so bottom-line, it looks something like this list called sheet_data:
sheet_data = [
('Intro', 'https://docs.google.com/spreadsheets/d/SHEET_ID/edit#gid=5'),
('XData', 'https://docs.google.com/spreadsheets/d/SHEET_ID/edit#gid=3'),
('YData', 'https://docs.google.com/spreadsheets/d/SHEET_ID/edit#gid=7')
]
You can then write them to the master (starting from the upper-left corner, cell A1) like this:
SHEET_ID = MASTER_SHEET_DRIVE_FILE_ID
SHEETS.spreadsheets().values().update(
spreadsheetId=SHEET_ID, range='A1',
body={'values': sheet_data},
valueInputOption='USER_ENTERED'
).execute()
Some caveats when using gid:
The first default sheet created for you (Sheet1) always has a gid=0.
Any sheets you add after that will have a random gid.
Don't bank on a gid=0 for the 1st sheet in your spreadsheets however as you or someone else may have deleted the original default sheet, like my example above.
If you want to see more examples of using the Sheets API, here are more videos I've made (along with posts that delve into each code sample):
Migrating SQL data to a Sheet plus code deep dive post
Formatting text using the Sheets API plus code deep dive post
Generating slides from spreadsheet data plus code deep dive post
Then when you open up the master in the Sheets UI, you can clickthrough to any of the individual sheets, regardless of which spreadsheet files they're in. If you want them automatically opened by another app or script, most programming languages offer developers a ways to launch a web browser given the target URL. In Python, it would be the webbrowser module (docs):
import webbrowser
webbrowser.open_new(url) # or webbrowser.open_new_tab(url)

Alternatively, you can try creating a custom function. With the spreadsheet open, click the Tools menu, then Script editor.... Paste the code into the editor:
/**
* Gets the Sheet ID from Sheet Name
*
* #param {string} input The Sheet Name
* #return The Sheet ID
* #customfunction
*/
function SHEETID(input) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var tab = ss.getSheetByName(input);
return tab.getSheetId();
}
Save, refresh the spreadsheet, and then type in your custom function
=SHEETID("Your Custom Sheet Name")
=SHEETID(A1)
And voila! The unique ID for the tab (in the current spreadsheet) is output. You can hyperlink to it by using the following formula:
=HYPERLINK("#gid="&SHEETID(A1),"Link")

In case you want to create a link to another sheet which will open the sheet in the same browser tab here is what you want to do:
1. Get the id of the sheet. Check the link in your browser and you will see #gid=x where x is the sheet id
2. Then you want to set the formula (hyperlink) to the cell and make it show as a hyperlink
SpreadsheetApp.getActiveSheet().getRange("A1").setFormula('=HYPERLINK("#gid=X","test")').setShowHyperlink(true);
If you don't use setShowHyperlink(true) it will be shown as a regular text.
This is basically a code version for the update provided by #rejthy above

Related

How to pin a common column on all the sheets

I am building a google sheet spanning multiple sheets. I have an index page with hyperlinks to few select sheets for convenience.
I'm looking to pin this column to show up on all the sheets.
Most suggestions I found online is about copying over the column from index page to all sheets, which I find hard to maintain and scale.
Is there a better way to achieve this?
Suggestion:
Perhaps you can add this sample bound script below to your spreadsheet file then save & run it from the editor:
To create a bound script in Google Sheets, open your spreadsheet and click Extensions or Tools > Apps Script or Script Editor
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var indexPage = ss.getSheetByName("Sheet").getRange("A:A");
var sheets = ss.getSheets();
sheets.forEach(cursheet => {
indexPage.copyTo(ss.getSheetByName(cursheet.getName()).getRange("A:A")); //Copies index on every sheets' column A
ss.getSheetByName(cursheet.getName()).setFrozenColumns(1); //// Freezes the first column on every sheet
});
}
This script will also automatically run every time you edit (that means every time you press Enter key after editing/adding a new cell value) your spreadsheet file as it was configured with onEdit trigger
Sample Result
After saving & running the script from the Apps Script editor:
Sheet:
Wish List
Laptops
Apparel
Reference:
Google Apps Script
Apps Script: setFrozenColumns(columns)

How to get all page names of a spreadsheet file in a column which will keep updating if the page name is changed?

I have a spreadsheet with say following page names:
"Sheet 1"
"Sheet 2"
"Sheet 3"
Afterwards, I changed name of
"Sheet 1" -> "Rough"
Is there anyway for me to get a list of all page names of the file in a spreadsheet page in a column?
Issue:
You want to trigger an action when a sheet name is modified (sheet here refers to a tab, or to a page, as you seem to call it). Sheets formulas will not get triggered by a sheet name change.
Solution:
You can accomplish this by installing an Apps Script onChange trigger, which will fire a function you specify when the spreadsheet's content or structure is changed (e.g., when a sheet name is changed). To do this, you can follow these steps:
In your spreadsheet, select Tools > Script editor to open a script bound to your file.
Create an Apps Script function to do the following: (1) retrieve all the sheet names, (2) write these sheet names to a specified sheet. You could use this function, for example. Copy it to the script editor and save the project:
function onChangeTrigger() {
var ss = SpreadsheetApp.getActive(); // Get current spreadsheet
var sheetNames = ss.getSheets().map(sheet => [sheet.getName()]); // Get sheet names
var destSheet = ss.getSheetByName("All Tab Names"); // Change according to your preferences
destSheet.getRange("A2:A").clearContent(); // Remove previous content
destSheet.getRange(2, 1, sheetNames.length).setValues(sheetNames);
}
Install the onChange trigger to fire the function above, either manually, following these steps, or programmatically, by running this function once:
function createOnChangeTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger("onChangeTrigger")
.forSpreadsheet(ss)
.onChange()
.create();
}
Note:
The function onChangeTrigger writes all sheet names to column A of a sheet called All Tab Names. Please change that to your sheet name. Also, check this if you want to change the range to which the sheet names are written.
Reference:
onChange trigger
You could install this Addon Formulas to use one of its functions =UTIL_SHEETNAME() within a cell of all your sheets (let say in A1 of every tab), then you would have a master sheet for listing your tabs and where you reference each formula you've used (=Sheet1!A1; =Sheet2!A1; etc...). But this function doesn't seem to update quickly though.
Alternatively, you could write a code (Apps Script) to list all your tabs, which has been answered everywhere. If you need help with the latter, add in your tags 'Google-Apps-Script' to add more visibility to your question.

Copy Row(s) To Outside Sheet Based On Data Validation

I am new to Google Sheet scripts and attempting to put together something for my business for project management purposes.
On edit, I'm looking to move a row to a new sheet outside the current workbook when a specific name is put in the cell T.
For Example:
if T says "John" - send the entire row to John's Google Sheet by ID
if T says "Amy" - send the entire row Amy's Google Sheet by ID
etc.
You can find an example of the sheet below.
Any and all help is appreciated.
sample spreadsheet
basically, this is the scenario: each of you (you, Amy and John) has own spreadsheet. you as "work assigner" have a master sheet with ID: 11n8TUEiuLLtbyd6ot5imX0zoAOLforHEBnefnHpoZhs (from the example you provided in your question) and all your data are in the sheet called Sheet1 dispersed across the range of cells: A1:AA (also from the example you provided in your question)...
now go to John's spreadsheet and paste this into cell A1:
=QUERY(IMPORTRANGE("11n8TUEiuLLtbyd6ot5imX0zoAOLforHEBnefnHpoZhs", "Sheet1!A1:AA"),
"select * where Col20='John'", 1)
next, go to Amy's spreadsheet and paste this into cell A1:
=QUERY(IMPORTRANGE("11n8TUEiuLLtbyd6ot5imX0zoAOLforHEBnefnHpoZhs", "Sheet1!A1:AA"),
"select * where Col20='Amy'", 1)
note: your spreadsheet can't be private. set permissions to: anyone with link can edit/view

Any way to fetch hyperlink of a column to another column on google spreadsheet?

I copy a list of company name from a website, each of them has its hyperlink.
But now I would like to paste the names on column B of a google spreadsheet and the link list on column C.
the sample spreadsheet shows here
Column B shows name and column C shows its link like http://.....
The =HYPERLINK function syntax are as followed
HYPERLINK(url, [link_label])
Is there any way I can make the [link_label] become link url itself?
Or is there any other way to list all the hyperlink of a sheet on a column?
The square brackets in a Google function indicate that the parameter is optional. HYPERLINK defaults to display the link itself if the [link_label] is omitted.
In other words, =HYPERLINK("www.example.com") will display as www.example.com.
See Google's documentation.

Google Sheets Export to Excel values instead of formulas

I have a google sheet where the cells in the first tab pull data from cells on the second tab.
for example Sheet1 cell A1 has =Sheet2!A1
This is true for every cell on Sheet1
When I do a File - Download As - Microsoft Excel (.xlsx)
It exports the cells with formulas. Is there a way to export the sheets as values and not formulas
In this case, Sheet1 cell A1 would not contain =Sheet2!A1 but the value of =Sheet2!A1?
You can copy your original google spreadsheet and, in the copy, change the formula for the first cell for each tab to import data from the original one:
=IMPORTRANGE("spreadsheet id","'tab name'!range")
Ex:
=IMPORTRANGE("1C-PS4wAHS8ssCNgVDfOsssREAz7PjuQGX23Rk0sssss","'measurement with spaces'!A12:F44")
The ID you can get via original spreadsheet URL:
https://docs.google.com/spreadsheets/d/1C-PS4wAHS8ssCNgVDfOsssREAz7PjuQGX23Rk0sssss/edit#gid=99999999
The exported xlsx file from the copy will have only the values
#fabceolins answer is simple and good for normal scenarios, i noticed however Excel will still contain reference to IMPORTRANGE formula which can cause access issues.
I created Google App script to copy in the following method.
If you can use Google App scripts, add the following functions:
function update_view(dup_id, TL="A1", BR="Z991") {
// Open current Sheet
var ss = SpreadsheetApp.getActiveSpreadsheet()
// Supply a duplicate google doc ID. This document will be exported to excel
var ds = SpreadsheetApp.openById(dup_id)
// UI element for notifying in the google sheets
var ui = SpreadsheetApp.getUi()
//Copy each sheet one by one
var sheets = ss.getSheets();
for (i=0; i<sheets.length; i++) {
src_sheet = sheets[i];
sheet_name = src_sheet.getName();
// If same sheet exists in the destination delete it and create an empty one
dst_sheet = ds.getSheetByName(sheet_name);
if (dst_sheet != null) {
ds.deleteSheet(dst_sheet)
}
dst_sheet = ds.insertSheet(sheet_name);
//set column width correctly
for(j=1; j<=src_sheet.getLastColumn(); j++){
dst_sheet.setColumnWidth(j, src_sheet.getColumnWidth(j))
}
src_range = src_sheet.getRange(TL + ":" + BR);
dst_range = dst_sheet.getRange(TL + ":" + BR);
//Note: DisplayValues is set as Values, formulas are removed in dup sheet
dst_range.setValues(src_range.getDisplayValues());
//Nice to haves for formatting
dst_range.setFontColors(src_range.getFontColors());
dst_range.setFontStyles(src_range.getFontStyles());
dst_range.setBackgrounds(src_range.getBackgrounds());
dst_range.setHorizontalAlignments(src_range.getHorizontalAlignments());
dst_range.setVerticalAlignments(src_range.getVerticalAlignments());
dst_range.setWraps(src_range.getWraps());
dst_contents_range = dst_sheet.getDataRange();
dst_contents_range.setBorder(true, true, true, true, true, true);
}
//Completed copy, Now open the dup document and export.
ui.alert("Backup Complete, Please open " + dup_id + " sheet to view contents.")
}
function update_mydoc_view(){
// https://docs.google.com/spreadsheets/d/<spreadsheet_id>/
update_view("<spreadsheet_id>")
}
To run the function, go to tools->macros->import , import the function and run update_mydoc_view().
After it is completed, export the google sheet into an excel document.
If you want to download a single sheet spreadsheet, instead of download it as .XLSX, download it as .CSV.
If by open the .CSV file by double clicking it shows strange characters, the default encoding of your computer is different from the one used by the Google servers, to fix this do the following:
Open Excel
Click File > Open
Select the .CSV file
The import wizard will be shown. One of the steps will allow you to select the file encoding, select UTF-8.
Once you finish with the import wizard save your file as .XLSX
Related
Microsoft Excel mangles Diacritics in .csv files?
Is it possible to force Excel recognize UTF-8 CSV files automatically?
I did the following and it worked for me :
Duplicate the file
CTRL+A > CTRL+X > CTRL + V Paste Value only (from the paste icon displayed after pressing CTRL+V)
If you want to keep only the values from Sheet1, just select the data in the sheet, copy it, open a new Excel work book and when you paste, rather than using the conventional hotkeys Ctrl+V, right click cell A1 and select Paste values under the Paste options category of the right click menu.
If the problem is that the downloaded Excel does not have functional formulas that take information from your second sheet and instead show the formulas as text, do what the previous commenter said. check your view to make sure you are not in formula view. use the hotkey Ctrl+ ~, or go to the view tab to check your view options.
By default this is what happens - you will see the values not the formula.
Are you sure you are not in formula view in Excel?
If you check "Show formula" it will switch in formula view.
Or generally speaking you can try those:
MS Excel showing the formula in a cell instead of the resulting value
I would be surprised if it was indeed a google sheet problem - it's about Excel display.

Resources