1

I need updated info from master sheets using query and importrange using apps script. This is master sheet. From which I need to access info: https://docs.google.com/spreadsheets/d/1tpv0iGwpnLUw6F5FoLn2jE4FBuF_igjriLA5uyNjczw/edit#gid=0

And this is sheet in which I need to show data: https://docs.google.com/spreadsheets/d/1ENZdtWI2f_wYzKbqUNziw6bVcTCpP6T4vkPbs-lPgC0/edit#gid=0

Currently I am using to fetch date. But I need to use Apps Script. =QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1tpv0iGwpnLUw6F5FoLn2jE4FBuF_igjriLA5uyNjczw/edit#gid=0","Task!A:J"),"SELECT* WHERE Col7 = 'JOHN'")

1 Answer 1

2

If I understand you correctly, you want to import data from a source spreadsheet to another spreadsheet when column G has a certain value.

In that case, you can use getValues() and setValues() for this:

const SOURCE_SPREADSHEET_ID = "1qkCxwyspeRrVcQRFqiHmIXRUjbrz9zgp09pYR0Y6_-M";
const TARGET_SPREADSHEET_ID = "1zp34NKnqRqdNvDzJx70T1spb0GbUQwnpUmJgkF81Pr4";
const SOURCE_SHEET_NAME = "Task";
const TARGET_SHEET_NAME = "Sheet1";
const NAME_COL_INDEX = 7;
const NAME = "JOHN";

function copyData() {
  const sourceSheet = SpreadsheetApp.openById(SOURCE_SPREADSHEET_ID).getSheetByName(SOURCE_SHEET_NAME);
  const sourceValues = sourceSheet.getRange("A1:J" + sourceSheet.getLastRow()).getValues();
  const targetValues = sourceValues.filter((row,i) => i === 0 || row[NAME_COL_INDEX-1] === NAME);
  const targetSheet = SpreadsheetApp.openById(TARGET_SPREADSHEET_ID).getSheetByName(TARGET_SHEET_NAME);
  targetSheet.clearContents();
  targetSheet.getRange(1,1,targetValues.length,targetValues[0].length).setValues(targetValues);
}

function installOnEditTrigger() {
  const ss = SpreadsheetApp.openById(SOURCE_SPREADSHEET_ID);
  ScriptApp.newTrigger("copyData")
           .forSpreadsheet(ss)
           .onEdit()
           .create();
}

Note:

If you want this to update automatically whenever users edit the source spreadsheet, consider installing an onEdit trigger. To do that in the example above, execute installOnEditTrigger once.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you. Can you please elaborate this code: filter((row,i) => i === 0 || row[NAME_COL_INDEX-1] === NAME); And one thing more. How can I give access using app script?
@user8702720 filter((row,i) => i === 0 || row[NAME_COL_INDEX-1] === NAME); means we want to filter out all data that is not the header row (i === 0) or whose G column value is JOHN.
Thank you. If I update source Sheet, target Sheet is not updating automatically. How can I do that? @Iamblichus
@user8702720 In that case, install an onEdit trigger. Take a look at my updated answer.
I have updated the script. But It still does not update the target sheet automatically when I edit the source sheet. Can you please look into the script in the target sheet? I have to run the script again to see updated data in the target sheet. @lamblichus
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.