Google Apps Script & Google Analytics API: Can't read PropertyIDs from Google Spreadsheet columns -
i'm trying use code in script editor read queries google spreadsheet columns people not know google apps scripts can use when want change queries start-date, end-date, metrics, dimensions, etc...the codes can read start-date, end-date, metrics, dimensions, , filters successfully. however, think doesn't recognize accountid, webpropertyid, , profileid sheet named "report configuration." got error "response code: 404. message: not found". me on this? below code got @eudardo:
var log_sheet_name = 'unsampled report logs'; var ss = spreadsheetapp.getactive(); var ui = spreadsheetapp.getui(); var sheet = ss.getsheetbyname('report configuration'); var startdaterange = sheet.getrange(10,5); var startdate = startdaterange.getvalue(); var enddaterange = sheet.getrange(10,6); var enddate = enddaterange.getvalue(); var metricsrange = sheet.getrange(10,8); var metrics = metricsrange.getvalue(); var dimensionsrange = sheet.getrange(10,9); var dimensions = dimensionsrange.getvalue(); var filtersrange = sheet.getrange(10,11); var filters = filtersrange.getvalue(); function insertreport() { var resource = { 'title': 'medium sale', 'start-date': startdate, 'end-date': enddate, 'metrics': metrics, 'dimensions': dimensions, 'filters': filters, }; var ss = spreadsheetapp.getactive(); var sheet = ss.getsheetbyname('report configuration'); var accountid = sheet.getrange(10,18).getvalue(); var webpropertyid = sheet.getrange(10,19).getvalue(); var profileid = sheet.getrange(10,20).getvalue(); try { var request = analytics.management.unsampledreports.insert(resource, accountid, webpropertyid, profileid); } catch (error) { ui.alert('error performing unsampled report query', error.message, ui.buttonset.ok); return; } var sheet = ss.getsheetbyname(log_sheet_name); if (!sheet) { sheet = ss.insertsheet(log_sheet_name); sheet.appendrow(['user', 'account', 'web property', 'view', 'title', 'inserted time', 'updated time', 'status', 'id', 'file']); sheet.getrange(1, 1, 1, 10).setfontweight('bold'); } sheet.appendrow([ session.geteffectiveuser().getemail(), request.accountid, request.webpropertyid, request.profileid, request.title, request.created, request.updated, request.status, request.id ]); } // scans log_sheet_name , tries update report pending function updateallreports() { var sheet = ss.getsheetbyname(log_sheet_name); var lastrow = sheet.getlastrow(); var datarange = sheet.getrange(2,1, lastrow, 10); var data = datarange.getvalues(); (var i=0; i<data.length; i++) { // if data pending let's try update it's status. it's complete // may take 24h process unsampled reprot if (data[i][0] == session.geteffectiveuser().getemail() && data[i][7] == 'pending') { try { var request = analytics.management.unsampledreports.get(data[i][1], data[i][2], data[i][3], data[i][8]); } catch (error) { ui.alert('error performing unsampled report query', error.message, ui.buttonset.ok); return; } data[i] = [ session.geteffectiveuser().getemail(), request.accountid, request.webpropertyid, request.profileid, request.title, request.created, request.updated, request.status, request.id, request.status == 'completed' ? driveapp.getfilebyid(request.drivedownloaddetails.documentid).geturl() : '' ]; // if data complete let's import new sheet if (request.status == 'completed') { importreportfromdrive(request.title, request.drivedownloaddetails.documentid); } } } // write once spreadsheet faster datarange.setvalues(data); } function importreportfromdrive(title, fileid) { var file = driveapp.getfilebyid(fileid); var csvstring = file.getblob().getdataasstring(); var data = utilities.parsecsv(csvstring); // find suitable name new sheet var i=1; var sheetname = title; while (ss.getsheetbyname(sheetname)) { sheetname = title + ' ('+ i++ +')'; } var sheet = ss.insertsheet(sheetname); var range = sheet.getrange(1, 1, data.length, data[0].length); range.setvalues(data); }
never mind. figured out why. it's because of mis-use of getrange(). it's supposed getrange(row,column), referenced getrange(column,row). silly me! :)
Comments
Post a Comment