在不降低代码速度的情况下移动数据的功能

时间:2018-11-05 10:22:41

标签: javascript google-apps-script google-sheets

我有代码,因此当我按checkbox时,代码将找到第一个空行并将数据放在那里,请参阅:

function onEdit(e) {
    const INPUT_ROW_INDEX = 6;
    if (INPUT_ROW_INDEX === 6) {
        const range = e.range;
        const spreadSheet = SpreadsheetApp.getActiveSpreadsheet(); `enter code here`

        const rowIndex = range.getRow();
        const columnIndex = range.getColumn();
        const value = range.getValue();

        if (value === true) {
            const startColumnName = columnToLetter(columnIndex - 6);
            const endColumnName = columnToLetter(columnIndex - 1);
            const firstFreeColumnName = columnToLetter(columnIndex - 2);

            const firstFreeRowIndex = getFirstEmptyRowByCell(firstFreeColumnName, INPUT_ROW_INDEX + 1, spreadSheet);

            const copyRange = spreadSheet.getRange(startColumnName + rowIndex + ":" + endColumnName + rowIndex);
            const deleteRange = spreadSheet.getRange(startColumnName + rowIndex + ":" + firstFreeColumnName + rowIndex);

            copyRange.copyTo(spreadSheet.getRange(startColumnName + firstFreeRowIndex + ":" + endColumnName + firstFreeRowIndex), {
                contentsOnly: true
            });
            deleteRange.clear();
            range.setValue(false);
        }
    }
}

function columnToLetter(column){
    var temp, letter = '';
    while (column > 0) {
        temp = (column - 1) % 26;
        letter = String.fromCharCode(temp + 65) + letter;
        column = (column - temp - 1) / 26;
    }
    return letter;
}


function getFirstEmptyRowByCell(firstFreeColumnName, startIndex, spreadSheet) {

    var range = spreadSheet.getRange(
        firstFreeColumnName + ':' + firstFreeColumnName);
    for (var i = startIndex; i <= range.getLastRow(); i++){
        if (!range.getCell(i, 1).getValue()) {

            break;

        }
    }
    return i;
}

我的问题是,如果存在某些功能或窍门,那么当我按下checkbox时,数据将在复选框行下方右移,而checkbox下的行将始终为空,然后我可以重复几次。

谢谢您的任何建议。

1月

0 个答案:

没有答案