0
votes

I'm having a hard time trying to find a way to sync multiple sheets into a master spreadsheet. For my test, I have a large spreadsheet with addresses in 5 cities. I am trying to create a spreadsheet for each city and be able to sync the data whenever it is changed in either sheet. So I would manage the overall data, and each of the 5 spreadsheets will be assigned to other people. That way, they can update the sheet w/o having access to all the data. The issue with using the builtin query or importrange functions are that if the user makes changes on the 'city' spreadsheets, it'll break the sheet because the data is referenced.

I found the code by 'Dev' below after doing a search.

var sourceSpreadsheetID = "ID HERE";
var sourceWorksheetName = "SHEET NAME HERE";
var destinationSpreadsheetID = "ID HERE";
var destinationWorksheetName = "SHEET NAME HERE";

function importData() {
  var thisSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetID);
  var thisWorksheet = thisSpreadsheet.getSheetByName(sourceWorksheetName);
  var thisData = thisWorksheet.getDataRange();
  var toSpreadsheet = SpreadsheetApp.openById(destinationSpreadsheetID);
  var toWorksheet = toSpreadsheet.getSheetByName(destinationWorksheetName);
  var toRange = toWorksheet.getRange(1, 1, thisData.getNumRows(), thisData.getNumColumns())
  toRange.setValues(thisData.getValues()); 
}

This works GREAT because it copies the data over allowing me to make edits, but the issue is I can't import just the data i want. If i could use a sql query, id do e.g. select * where city = 'miami' but I believe this is not possible with google spreadsheets at the moment.

So the next idea I had was to use this script and set filters on the child spreadsheets. The issue is when it runs the script, it will copy the entire sheet back and if any of the 5 users are updating their own spreadsheets at same time, it would cause an issue as they would be overwriting each others changes. The other thought I had was that since each record has its own unique id#(i have a column called id) is there a way to tell the active spreadsheet to update the row in the mastersheet where id = masterspreadsheet.id on edit?

Any help in the right direction would be greatly appreciated.

2
You can actually use QUERY in google spreadsheets, but it will be dynamic, as in the data will always be updating: support.google.com/docs/answer/3093343?hl=en. does this solve your issue? - Kriggs
I am pulling data from a separate spreadsheet, so I cannot use the Query function by itself. I have to do an importrange which references the data. So unfortunately, this does not work for me. - User125

2 Answers

0
votes

You can import all the data, then use the JavaScript sort() method on a array.

var thisData = thisWorksheet.getDataRange();

var arryOfData = thisData.getValues();
Loggger.log('arryOfData: ' + arryOfData[0]);

var dataSorted = thisData.sort();
Logger.log('dataSorted: ' + dataSorted[0]);

Run the code, then VIEW the LOG to see what was returned into the variables as a way to debug the code.

Then you could use indexOf() and slice() to remove parts of the data that you don't want.

0
votes

Solution for those who have to use multiple Google Sheets AND Excel:

I have users editing their own files on Google Drive. My Master sheet combines all the necessary info into one. Google has built-in command in google sheets to get data from specific locations in other sheets. Create a master google sheets document to pull all the relevant info into 1 document. This protects all the other information you don't want to query and brings it all into 1 google sheet.

(I changed some letters for privacy reasons, this is just an example:) Put this is the cell where you are importing the data, i.e. A1. It will import data from the sheet named Totals, from A1 to C2.

=IMPORTRANGE("1l9jPPp5ylfgAjO_PDymXSR_ivfwHG1j7Ax-UASi6UYc","Totals!A1:C2")

=IMPORTRANGE("Put-Your-Document-Key-Here","PutSheetNameHere!Put the range or a single cell here.")

My master sheet is published so I can use a query to import it into excel.

You get the document key from the URL. Open the Google sheet you want to import data from and the URL should be something like this:

https://docs.google.com/spreadsheets/d/1l9jPPp5ylfgAjO_PFymXSR_ivfwHG1j7Ax-UASi6UYc/edit#gid=0

In the document settings you can make it check and update every minute.

THEN: You can do 1 web query into Excel.

OR

If you are using only excel, just make a shared google drive or share the document into your drive. Install Google Drive on your computer, then just reference the excel file on your drive like normal. Google drive will keep it updated. The simplest solution is making everyone use excel and then sharing the files to your computer-synced drive. After that just use normal references to other excel files which will be in a constant place on your drive.

http://www.officetooltips.com/excel/tips/referencing_cells_outside_the_worksheet.html