Ask Your Question
1

How can I incorporate the importRange feature into a Javascript function for Google Sheets?

asked 2022-11-23 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-06-19 11:00:00 +0000

djk gravatar image

Here is an example of a Google Apps Script function that uses the importRange function to retrieve data from another sheet:

function myFunction() {
  var sourceSheetUrl = 'https://docs.google.com/spreadsheets/d/1234567890abcdefghijklmnopqrstuvwxyz/edit#gid=987654321';
  var sourceSheetRange = 'Sheet1!A1:C10';
  var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet2');
  var importedRange = SpreadsheetApp.openByUrl(sourceSheetUrl).getRange(sourceSheetRange).getValues();
  targetSheet.getRange(1, 1, importedRange.length, importedRange[0].length).setValues(importedRange);
}

In this example, the importRange function is used to get the values from a range in a sheet specified by sourceSheetUrl and sourceSheetRange. The imported range is then copied to Sheet2 in the active spreadsheet using the setValues method.

Note that this function uses Google Apps Script, which is a scripting language based on Javascript that is used to automate tasks in Google Sheets and other Google apps. To create and run this function, you'll need to open the script editor in Google Sheets (Tools > Script editor), paste in the code, save it, and then run it from the script editor (Run > myFunction).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-11-23 11:00:00 +0000

Seen: 11 times

Last updated: Jun 19 '22