0

我最近创建了一个脚本,它查看一个电子表格并创建一个我们想要的项目列表。然后它在另一个电子表格中查找这些项目并创建匹配对象的数组。然后对象按其名称属性排序。最后它们被输出到不同的工作表上,但这在这里无关紧要。

在 JS 中执行此操作的最佳方法是什么?有没有比我用过的更有效的方法(我确定有)。

    //get basic information about the spreadsheet

var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var rows = new Array();
var numRows = new Array();


for (var i = 0; i < sheets.length; i++) {
    rows[i] = sheets[i].getDataRange();
    numRows[i] = rows[i].getNumRows();
}


//Create an object for each company with suppressed and total statements variables
//object will have properties name, supressed integer, total integer, and a percentage calculated through a percentage() method
function companySupressionStat(name, supressed, total) {
    this.name = name;
    this.supressed = supressed;
    this.total = total;



}
var objectNames = new Array();
// find the column with the supression data
function findSupression() {
    var supressedColumn;

    // check every column on the 3rd row for the word supressed column
    for (var l = 1; l < sheets[1].getLastColumn(); l++) {

        if (sheets[1].getRange(3, l).getValue() == "Supr.ed Stmts") {
            supressedColumn = l;
            break;
        }
    }

    return supressedColumn;

}

//find company name column

function findCompany() {
    var companyColumn;

    for (var l = 1; l < sheets[1].getLastColumn(); l++) {
        if (sheets[1].getRange(1, l).getValue() == "SLA Legend") {
            companyColumn = l;
            break;
        }
    }
    return companyColumn;

}
// check if we have missing value, then identify them in an array of strings called listErrors
function listErrorChecker(listLength, objectLength, objectNames, list) {
    var listErrors = new Array(); // create a variable to keep track of our missing company's
    if (listLength > objectLength) { //if our list is longer than our array of objects

        for (var l = 0; l < list.length; l++) {
            Logger.log(objectNames.indexOf(list[l]));
            if (objectNames.indexOf(list[l]) == -1) {
                listErrors.push("The company " + list[l] + " is missing from our output! Please check the spelling and case of " + list[l].toUpperCase() + " and make sure it is the same as the report we are looking in.");
            }
        }
        return listErrors;
    }
    else {
        listErrors = "no errors";
        return listErrors;
    }

}

//Create an array list of all the company's we want to track

var list = new Array();
for (var k = 1; k <= numRows[0]; k++) {
    list.push(sheets[0].getRange(k, 1).getValue().toLowerCase()); //assumes that our list is in our first spreadsheet, in the first column

}

//Go down the spreadsheet on the column w/ the word "suppression". start creating new objects with the the correct properties if the company name is on our list of company names.


var arrayOfObjects = new Array();
var companyColumn = findCompany();
var supressedColumn = findSupression();
var lastRow = sheets[1].getLastRow();

for (var m = 1; m <= lastRow; m++) {
    if (sheets[1].getRange(m, companyColumn).getValue() !== "" && list.indexOf(sheets[1].getRange(m, companyColumn).getValue().toLowerCase()) !== -1) { //if the company name is not blank and if the company name is in our list, create a new instance of the compression stat object
        arrayOfObjects.push(new companySupressionStat(
        sheets[1].getRange(m, companyColumn).getValue(),
        sheets[1].getRange(m, supressedColumn).getValue(),
        sheets[1].getRange(m, supressedColumn + 2).getValue()));
        objectNames.push(sheets[1].getRange(m, companyColumn).getValue().toLowerCase()); //keep track of the object's names in a simple arrray of strings for error checking later
    }
}

// sort our array of objects by name of company alphabetically
function sortOn(property) {
    return function(a, b) {
        if (a[property] < b[property]) {
            return -1;
        }
        else if (a[property] > b[property]) {
            return 1;
        }
        else {
            return 0;
        }
    };
}


arrayOfObjects.sort(sortOn("name")); // call the sorting function
4

1 回答 1

0

我肯定会将 list 和 objectNames 列表更改为对象,然后我会使用对象属性(如 C# 中的 HashSet)。

例如,我会将列表更改为:

var list = {};
for (var k = 1; k <= numRows[0]; k++) {
    list[sheets[0].getRange(k, 1).getValue().toLowerCase()]=1;
}
....
....
....
for (var m = 1; m <= lastRow; m++) {
    if (sheets[1].getRange(m, companyColumn).getValue() !== "" && list[sheets[1].getRange(m, companyColumn).getValue().toLowerCase()]) {

通常搜索数组比查找对象属性要慢。

在此处查看:阵列性能

于 2013-10-04T21:11:39.143 回答