Connect two Google Sheets with script - google-apps-script

I want to create a sheet by SpreadsheetApp.create(); and to fill a QUERY in this new generated sheet via cell.setFormula and IMPORTRANGE to get data of another Sheet.
It doesn't work because the sheets are not connected. If I do so manually the QUERY works.
Is there a way to connect two sheets via script?

You could create a variable with the locations of the sheets by the ID's. Then, access the sheet remotely via the script. Save the cell variable and paste this over into the first sheet.
Psuedo-Code Example
Sheet1 = SpreadsheetApp.getSheetByID('IDofSheetOne');
SpreadsheetApp.create();
Sheet2 = SpreadsheetApp.getSheetByID('IDofSheetTwo'); // the one just created, you'd probably have to use some kind of get to get this ID.
Now from here, you can just say, Sheet1.doWhatever() and Sheet2.doWhatever().
Thats how you can access more than one sheet at a time.

Related

Transfer Google Spreadsheet Cell Value generated from ImportRange to another sheet and cell automatically

I have sheet named origin that displays set of data Col A:B via ImportRange
I have a sheet named destination
What I wanted to do:
When ImportData changes (a new item is added in the origin sheet) I wanted to copy that
item and paste it in the destination sheet.
What I dont want to happen:
Is copy data that are already existing in the destination sheet.
When ImportData Origin is sorted, the display in the origin sheet is
also changed. In this case I should still be able to copy data that
are not existing in the destination sheet even if both sheets are no
longer sorted the same.
Any idea on how I can achieve this?
Thank you so much
Explanation:
I would advice you to use a formula in the destination sheet, something like:
UNIQUE(IMPORTRANGE("url", "origin!A1:B"))
so you can have the unique items of the origin sheet in the destination sheet.
The reason that I am suggesting that is because you are looking for a trigger behaviour. Namely, when a new value is added in a cell, grab this value and send it to another spreadsheet. The problem with this approach is that Google-Apps-Script triggers are not triggered by scripts nor formulas, but only by user edits. In other words, if a cell is modified by your import range formula, there is not a direct GAS solution which can figure that out. Only workarounds exist which might have limitations and they are very specific to your exact use case.
Workarounds
See this thread for some info and references.
You can use the Properties Service which you can store some sort of data that is bound the script. In this way, you can create a Time-driven trigger which will run every 1 minute or so and check if there are changes to the original data. If a new value is added, update the property and send the data to the destination sheet. If no new data is added, do nothing.

How to protect the cell/range at the same time as appendrow

Im working on a website that stores data in a google spreadsheet and was wondering how I could use the appendrow() function and then protect that cell so no one can access it.
I'm new to google apps script and have been searching the internet for the last hour or so and can't find anything.
sheet.appendRow([text]);
But after that I would like it to get the value of that cell that was just written on so I could write:
var protection = the-row-just-written-on.protect()
I I am hoping for the result to be that no one can delete the text once it has been written on.

How to copy Google pivot table (values and format) to another spreadsheet using appscript?

I have a spreadsheet(say A) which contains a pivot table in one of the sheets.
Another sheet contains the source data for that pivot table.
I want to only copy the table from the sheet to another spreadsheet(say B) as I have to send the spreadhsheet B containing the single sheet in email.
I tried the following solutions suggested in the similar issue link below but they don't seem to solve my problem:
How to copy a row from one google spreadsheet to another google spreadsheet using google apps script?
Copy sheet from spreadsheet A to spreadsheet B. This failed because
the source data sheet was not in B.
Tried range.copyTo() but this failed too because it only works if
applied on the same spreadsheet.
Copied values i.e used getValues() and setValues(). This might work
but I also want the format to be copied.

Switch from one spreadsheet to another in the same tab in Google Sheets with a script

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

Some spreadsheet cells not updated before trigger function sends email

I have a spreadsheet which extracts and accumulates all the required data from other 6 spreadsheets using Vmerge and Query formulas and all the consolidated data will be converted to pdf and emaild to mail ids using trigger event.
Here begins the problem every time the attachment mail posted consist all the headers and other format, but the data which is extracted does not appear. It seems to appear like - to open the same spreadsheet, after a while all the - (hyphens) are replaced by the data / Hope all the data updates after a while of opening the spreadsheet.
link for sheet
Can anyone direct me to make this issue sorted out.
For this solution should be -> all the data should be updated and then the email script should work; or either it should update before emailing script starts.
Or any other better ideas are appreciated.
Some spreadsheet formulas, like ImportRange and ImportXml (and also Apps Script custom formulas) are only evaluated when there's someone logged in the spreadsheet. It's like these functions need an account to be evaluated from, for example, in importRange the account logged in must have access to the range being imported, if you share this spreadsheet with someone but not the importRange source, when this person is viewing this spreadsheet, the importRange function will not work (well, unless you're also in the spreadsheet and the formulas have already been evaluated).
Bottom line is, you can't have this formulas and use a script triggered on time-driven (or other trigger that does not require someone logged in) and expect the script to be able to read this data.
The workaround though is quite simple. Do what the importRange function does inside your script! e.g.
var source = SpreadsheetApp.openById('source-spreadsheet-key');
var data = source.getSheetByName('List').getRange('I6:AT500').getValues();
//then save it somewhere
var s = Spreadsheet.getActive().getSheetByName('hidden-import');
s.getRange('I6:AT500').setValues(data);
SpreadsheetApp.flush(); //force the data to be written
//so all the other formulas on your spreadsheet get updated with the new data
All your "logic" formulas, like query and vmerge, which are difficult for the script to mimic, can be left on the spreadsheet, but reference this "hidden-import" sheet I just invented instead of nesting importRange directly.
[edit]
To copy only non-empty rows do like this:
var data = SpreadsheetApp.openById('source-spreadsheet-key').
getSheetByName('List').getDataRange().getValues();
Spreadsheet.getActive().getSheetByName('hidden-import').
getRange(1,1,data.length,data[0].length).setValues(data);