I do price parsing with this function:

function myFunction (pos, url) { var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange(pos); var cell = range.getCell(1,1); try { var response = UrlFetchApp.fetch(url); var textResp = response.getContentText(); var start, end, value; start = textResp.indexOf('"price":"', end) + 9; end = textResp.indexOf('.00","list"', start); name = textResp.substring(start, end); cell.setValue(name); cell = cell.offset(0,1); start = textResp.indexOf('item_avb availability_', end) + 22; end = textResp.indexOf('"><link itemprop', start); value = textResp.substring(start, end); if (value == 'yes') cell.setValue('есть') else cell.setValue('на заказ') } catch (err) { cell.setValue('404!!!'); } } 

Reference sheet = SpreadsheetApp.getActiveSheet()
And how will you turn to the sheet of the document not from which you went to the editor, but by name? There are several sheets in the document and sometimes the script overwrites me with not the right thing ...
And through try catch , processing is implemented if the URL is not accessible, but somehow it looks clumsy. Can someone tell me how poetic?

  • one
    If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

To select a sheet by name, you must first take a pointer to a table ( getActiveSpreadsheet ), and then use the getSheetByName method. Example:

 var spr = SpreadsheetApp.getActiveSpreadsheet(); var sheet = spr.getSheetByName('Sheet1'); 

By the way, you can refer to other tables : see openById .


Instead of try ... catch when using UrlFetchApp.fetch, it is better to set the muteHttpExceptions: true parameter. If the request was unsuccessful, script execution will continue, and the response code (404, 500, 503, etc.) will be returned in an object of the HttpResponse class. Example:

 var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true}); if (response.getResponseCode() == 200) { // обработка данных } else { Logger.log(response); // неудача return; }