Is it possible to get values from range A1:B1 and A5:B5 with one Sheets API call in…
- User
Is it possible to get values from range A1:B1 and A5:B5 with one Sheets API call in Google App scripts?
Assistant
Yes, you can get values from non-contiguous ranges like A1:B1 and A5:B5 with a single Sheets API call in Google Apps Script using the
Sheets.Spreadsheets.Values.batchGet()method.Here's how to do it:
javascriptfunction getNonContiguousRanges() { const spreadsheetId = "YOUR_SPREADSHEET_ID"; const ranges = ["Sheet1!A1:B1", "Sheet1!A5:B5"]; const response = Sheets.Spreadsheets.Values.batchGet( spreadsheetId, {ranges: ranges} ); const valueRanges = response.valueRanges; // Access the values from each range const firstRangeValues = valueRanges[0].values; const secondRangeValues = valueRanges[1].values; Logger.log(firstRangeValues); Logger.log(secondRangeValues); }Would you like me to explain or break down this code?
- User
It is web api, can I somehow use GAS Sheets Api?
Assistant
Yes, you can use the built-in Google Apps Script Spreadsheet service instead of the Sheets API. Here's how to get values from non-contiguous ranges using the native GAS methods:
javascriptfunction getNonContiguousRanges() { const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getActiveSheet(); // Get values from both ranges const range1Values = sheet.getRange("A1:B1").getValues(); const range5Values = sheet.getRange("A5:B5").getValues(); // Combine results if needed const allValues = { range1: range1Values, range5: range5Values }; Logger.log(range1Values); Logger.log(range5Values); return allValues; }Would you like me to explain or break down this code?