我是新来的javascript和scrip编辑器。我自己解释了一下逻辑,一步步把事情分解开来,已经走得很远了。但还有最后一部分难题,我似乎无法解决。我想让脚本 归到第一个空栏 的第16行,并给我该单元格的行+列。所有的代码都是有效的,但是,当它到了脚本的第5步时,发生了什么事情呢?它将 "it worked "放置在行的最后一列,而不是第一列。.
function refreshCell(){
var app = SpreadsheetApp.openById("14FgHQieAOCko3pwzLSUnO5Ky5EuLN88r5Y4LnuMKREI");
var sheetName = app.getSheetByName("FinancialGoals");
var cellContainingFormula = sheetName.getRange("J14");
/* 1. Clear cell containing formula */
cellContainingFormula.clear();
/* 2. auto-run functon refreshCell on the first of the month: edit > project triggers > add new trigger */
/* 3. Add new formula to cell */
cellContainingFormula.setValue('=10248-A16');
/* 4. Add formatting to cell: set background color, textcolor green, text bold, center text */
cellContainingFormula.setBackground('#B7B7B7').setFontColor('#38761d').setHorizontalAlignment('center');
/* 5. Navigate to the first empty column of row */
var lastColumn = sheetName.getLastColumn();
var activeCell = sheetName.getRange(16, lastColumn).activate();
activeCell.setValue("it worked")
}
试试这个。
function refreshCell(){
var ss=SpreadsheetApp.openById("ssid");
var sh=ss.getSheetByName("FinancialGoals");
var cellContainingFormula=sh.getRange("J14");
cellContainingFormula.clear();
cellContainingFormula.setValue('=10248-A16');
cellContainingFormula.setBackground('#B7B7B7').setFontColor('#38761d').setHorizontalAlignment('center');
var col=sh.getRange(16,1,1,sh.getLastColumn()).getValues()[0].reduce(function(a,c,i){if(c=='' && !a.found) {a.col=i+1;a.found=true;}return a;},{col:0,found:false}).col;
sh.getRange(16,col).setValue("it worked");
}