Following this guide, I am able to link data from my Google Sheet into my Google Doc.
https://support.google.com/a/users/answer/9308662?hl=en
Is there a way to have range size update automatically when the table shrinks and grows?
You can do the following:
Create a new named range that includes the current range of the table (ref: Name a range of cells).
Link that named range to your desired location, using Link to data in a spreadsheet.
On your spreadsheet, click Tools > Script editor to open a bound script, and copy the following code. This function retrieves your desired named range and updates it with the current dimensions of your table:
const SHEET_NAME = "Sheet1"; // Change according to your preferences
const NAMED_RANGE_NAME = "MY_RANGE"; // Change according to your preferences
function updateNamedRange() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(SHEET_NAME);
const namedRanges = ss.getNamedRanges();
const namedRange = namedRanges.find(namedRange => namedRange.getName() === NAMED_RANGE_NAME);
const range = sheet.getRange("A1").getDataRegion();
namedRange.setRange(range);
}
Install a time-driven trigger which will fire updateNamedRange with the specified periodicity. You can do that manually (following these steps), or programatically. In order to install this programmatically, copy and run a function like this once:
function installTimeTrigger() {
ScriptApp.newTrigger("updateNamedRange")
.timeBased()
.everyMinutes(1)
.create();
}
Note:
The previous sample is using everyMinutes with the parameter set to 1, so the range will be updated every minute. You can find methods for alternative frequencies here.
In the sample above, the table is in a sheet named Sheet1, with a named range title MY_RANGE, and it starts at cell A1. Change all those in your spreadsheet if that's not your case.
I'm assuming you already know how to link named ranges.
Related
I use importrange for combine 2 google sheet Target A and Source B
In sheet A =
Importrange(“linkgooglesheetB”,”SheetB!!A2:M1000)
It’s done
But when data in sheet Source B cleared/changed, then
Data in sheet Target A cleared/changed too
What formula can use same my above formula but data in Target A do not change after combined (A&B), when B changed.
Answer:
You can do this with Google Apps Script.
Explanation:
The IMPORTRANGE formula will always automatically update when the range from which it is importing had a data change. In fact, this is a feature of all sheet formulae; they are designed to keep everything up-to-date when data changes.
For this reason, you can not use a formula. You will have to do this using Google Apps Script.
Example code:
The idea is as such:
When the sheet is edited, a script will run.
If the cell data matches a certain pattern, then the script will paste in the data permanently, in the same way that IMPORTRANGE works.
function runOnEdit(e) {
// We will make the patten here. In this case, the text entered in the cell must be of the form:
// "IMPORT(link,range)
// example:
// IMPORT,https://docs.google.com/spreadsheets/d/some-id/edit, SheetB!A2:M1000
const patternStart = "IMPORT"
const compareVals = e.value.split(",")
if (compareVals.length != 3 || compareVals[0] != patternStart) return
try {
const ss = SpreadsheetApp.openByUrl(compareVals[1])
const importRange = compareVals[2].split('!')
const sheetName = importRange[0]
const range = importRange[1]
const sheet = ss.getSheetByName(sheetName)
console.log(sheetName)
console.log(range)
const data = sheet.getRange(range).getValues()
const formatting = sheet.getRange(range).getTextStyles()
const activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
const row = e.range.getRow()
const col = e.range.getColumn()
activeSheet.getRange(row, col, data.length, data[0].length).setValues(data).setTextStyles(formatting)
}
catch (err) {
e.range.setValue(err)
}
}
Things to note:
The pattern can not start with an = as this will be read as a formula by Sheets and throw an error.
This must be set up as an installable trigger, as opening a separate Spreadsheet requires authorisation, which can not be done from a simple trigger.
You can install the trigger by clicking on the clock logo in the left toolbar from the Apps Script editor, then clicking + Add Trigger in the bottom right and using the following settings:
Choose which function to run: runOnEdit
Which runs at deployment: Head
Select event source: From spreadsheet
Select event type: On edit
Visual example:
Most of the formula is live update, including query function. However in order not to have your data change for importrange, there is two possible solution:
1)Create a backup sheet by copy paste data from the main sheet, and importrange from the backup sheet, so any change in main sheet will not have impact on your result
2)Write Google App Scrip to copy data, but it take time to learn
I have a spreadsheet that contains four different sheets
Day1 | Day2 | Day3 | Summary
in the summary sheet i will be referencing a cell in either Day1,Day2,Day3 sheets
for example i'm using this formula in the summary sheet to get a value from Day3 sheet
='Day3'!$D2
so far its working however i want the sheet name (in this case the 'Day3' in the formula above) to be static.
meaning if i rename the Day3 sheet into something else for example rename it to Day4, and create another sheet named Day3,
i want the formula above to still reference ='Day3'!$D2
because as of now if i do the steps i mentioned above, ='Day3'!$D2 automatically changes to ='Day4'!$D2 when i rename the Day3 sheet
is this possible in google sheets?
This is an example of recalculation in Google Sheets, and for your use case, there is currently no way of disabling this feature.
As a workaround, you can use Google Apps Script to create an installable trigger to change the sheet reference manually everytime the sheet structure changes.
Sample Code:
function createTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger("retainReference")
.forSpreadsheet(ss)
.onChange()
.create();
}
function retainReference() {
var sheet = SpreadsheetApp.getActive().getSheetByName("Summary");
var range = sheet.getRange('A1');
range.setFormula("='Day3'!D2");
}
Note: You only need to run createTrigger() function manually.
Sample Output:
When renaming sheet Day3 to sheet Day4:
Reference:
Installable Triggers
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.
I saw several complains about the delay of updating data through IMPORTRANGE in Google Sheets but I need the opposite and don't want the second sheet to get updated automatically, just update at the end of the day for example.
The code is already like this:
=IMPORTRANGE("The Key","The_Page!B:D")
I hacked around this by, on both spreadsheets by creating a refresh loop using a NOW cell, with both spreadsheets crossreferencing each other's NOW cell.
When the original sheet gets appended with a form submission or something, it updates its own NOW cell, and reupdates own IMPORTRANGE cell. The second spreadsheet follows suit, updating its own NOW cell to provide the original sheet with the correct data. Because the second spreadsheet has updated itself, it also updates the main IMPORTRANGE which refreshes the data you want it to display in the first place, as well as the IMPORTRANGE cell which gets the NOW cell from the original spreadsheet
At least, I'm pretty sure that's how it works. All I know, and all I care about, frankly, is that it works
Maybe you need to use the script editor and write a simple function of the kind:
function importData()
{
var ss = SpreadsheetApp.getActiveSpreadsheet(); //source ss
var sheet = ss.getSheetByName("The_Page"); //opens the sheet with your source data
var values = sheet.getRange("B:D").getValues(); //gets needed values
var ts = SpreadsheetApp.openById("The Key"); //target ss - paste your key
ts.getSheetByName("Name of the target sheet").getRange("B:D").setValues(values);
}
And then add a time-driven trigger to this project (Resources > Current project's triggers > Add a new one).
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