问题
I'm trying to use this code in Script Editor to read the queries from Google Spreadsheet columns so that people who do not know Google Apps Scripts can use it when they want to change the queries like start-date, end-date, metrics, dimensions, etc...the codes can read start-date, end-date, metrics, dimensions, and filters successfully. However, I think it doesn't recognize the accountId, webPropertyId, and profileId from a sheet named "Report Configuration." I got an error "Response Code: 404. Message: Not Found". Could you help me on this? Below is the code I got from @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 and tries to update any report that is 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();
for (var i=0; i<data.length; i++) {
// If data is PENDING let's try to update it's status. Hopefully it's complete now
// but it may take up to 24h to process an 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 is Complete let's import it into a new sheet
if (request.status == 'COMPLETED') {
importReportFromDrive(request.title, request.driveDownloadDetails.documentId);
}
}
}
// Write only once to the spreadsheet this is faster
dataRange.setValues(data);
}
function importReportFromDrive(title, fileId) {
var file = DriveApp.getFileById(fileId);
var csvString = file.getBlob().getDataAsString();
var data = Utilities.parseCsv(csvString);
// Find a suitable name for the 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);
}
回答1:
Never mind. I just figured out why. It's because of my mis-use of getRange(). It's supposed to be getRange(row,column), but I referenced it as getRange(column,row). Silly me! :)
来源:https://stackoverflow.com/questions/36781302/google-apps-script-google-analytics-api-cant-read-propertyids-from-google-sp