mardi 23 décembre 2014

if (identical values between 2 array indices) { set data in corresponding spreadsheet }


Summary of the script: Scrapes basic email information (date sent/received, to, from, subject, email label) and logs this data into spreadsheets. The spreadsheets are named after email labels, all emails that belong to a certain label get their information logged to that corresponding spreadsheet.


The spreadsheet names and Ids are organized in the array of arrays fileNames as [[spreadsheet name, id],[spreadsheet name, id],[etc, etc]]


Below is some of this array setup.



function testFunction(){

var emailByLabel = ["just", "a", "list", "of", "data"];

// ** CREATE AN ARRAY OF ARRAYS.
// LAYER 1- [LIST OF SPREADSHEETS]
// LAYER 2- [SPREADSHEET NAME, SPREADSHEET ID]
var fileNames = [];
var folders = DriveApp.getFoldersByName("email-data");

while (folders.hasNext()) {
var folder = folders.next();
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
fileNames.push([file.getName(), file.getId()]);
}
}


It is here that I am trying to "match" spreadsheet names to label names, and "set" data to corresponding spreadsheets when there's a match. Instead what I am getting is all data in all spreadsheets.



for(var l=0;l<fileNames.length;l++){
for(var i=0;i<emailByLabel.length;i++){
if(emailByLabel[i][1] == fileNames[l][0]){

var ss = SpreadsheetApp.openById(fileNames[l][1]).getSheets()[0];
Logger.log(fileNames[l][0]);
Logger.log(fileNames[l][1]);

ss.getRange(2,1,emailByLabel.length,5).setValues(emailByLabel);

} else {
Logger.log("MATCH, BROKE!");
Logger.log(fileNames[l][0]);
Logger.log(fileNames[l][1]);
}
}
}
}


I have tried replacing



ss.getRange(2,1,emailByLabel.length,5).setValues(emailByLabel);


with



ss.getRange(2,1,emailByLabel[i].length,5).setValues(emailByLabel[i]);


but i receive the error Cannot convert Array to Object[][]


Full Code with some highlighted, relevant lines of code: http://ift.tt/1xBXtPq





Aucun commentaire:

Enregistrer un commentaire