Created
March 8, 2013 10:58
-
-
Save freshfey/5115708 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function dailyStatsParser() { | |
var ss = SpreadsheetApp.openById("0AhDr-PBlhQuYdExJbFNCVkJUa28ybHRVMEUyaVdsUWc"); | |
var now = new Date(); | |
var yesterday = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1, now.getHours()); | |
var yesterdayString = Utilities.formatDate(yesterday, "CET", "yyyy-MM-dd"); | |
var fileNames = new Array("Tutti.ch stats ", "Tutti.ch (de) stats ", "Tutti.ch (fr) stats ", "Tutti.ch (it) stats "); | |
var docIDs = new Array(); | |
var doclist = DocsList.getRootFolder().getFiles(); | |
for (f in fileNames) { | |
fileNames[f] = fileNames[f] + yesterdayString; | |
for (nn = 0; nn < doclist.length; ++nn){ | |
if ( doclist[nn].getName().toString() == fileNames[f] ) { | |
docIDs.push(doclist[nn].getId()); | |
} | |
} | |
} | |
for (d in docIDs) { | |
var doc = DocumentApp.openById(docIDs[d]); | |
var lang = DocsList.getFileById(docIDs[d]).getName().match(/\((\D{2})\)/); | |
if (lang !== null) {lang = lang[1]; } else lang = "ALL"; | |
// Obtain all paragraphs. | |
var pars = doc.getParagraphs(); | |
var dataArray = new Array(); | |
var dAInd = 0; | |
for (p in pars) { | |
if (pars[p].getText().substring(0, 1) == "K") { // Check if line starts with K and ignore all others | |
dataArray[dAInd] = pars[p].getText().substring(0, 4); | |
var m = pars[p].getText().match(/:\s+(\d+(?:,\d+)?)/); | |
if (m[1].indexOf(",") !== -1 ) m[1] = m[1].replace(",", "."); // replace , with . | |
dataArray[dAInd + 1] = m[1]; | |
dAInd = dAInd + 2; | |
} | |
} | |
var outputSheet = ss.getSheetByName("Output"); | |
var outputHeader = outputSheet.getRange(1, 1, 1, outputSheet.getLastColumn()).getValues(); | |
var outputData = new Array(); | |
// outputData.push(yesterdayString, languages[l].toUpperCase()); | |
outputData.push(yesterdayString, lang.toUpperCase()); | |
for (i in outputHeader[0]) { | |
// search for the corresponding "Kxxx" in the header and returns its index | |
var t = dataArray.indexOf(outputHeader[0][i]); | |
if (t > -1 ) outputData.push(dataArray[t + 1]); | |
} | |
if (outputData.length > 0) { | |
/* setValues expects a matrix as parameter i.e. a two dimension array, and you're passing a simple array. | |
setValues does not care if your range is just one row, one column, a single cell or the whole sheet, you must pass a matrix. | |
So the solution is just adding an extra pair of brackets (since it's one row). */ | |
outputSheet.getRange(outputSheet.getLastRow() + 1, 1, 1, outputData.length).setValues([outputData]); | |
} | |
// Create a copy of the file before deleting it | |
/* | |
var arcFolder = DocsList.getFolder("Archive"); | |
var fileID = DocsList.getFileById(docIDs[d]); | |
fileID.makeCopy().addToFolder(arcFolder); | |
*/ | |
// Delete the files after they've been parsed | |
DocsList.getFileById(docIDs[d]).setTrashed(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment