【问题标题】:Get the first empty column of the row.... then log that cell's row + column获取该行的第一个空列....然后记录该单元格的行+列
【发布时间】:2020-06-10 22:11:27
【问题描述】:

我是 javascript 和脚本编辑器的新手。通过向自己解释逻辑并逐步分解事情,我已经走了很远。但是我似乎无法解决这个难题的最后一部分。我希望脚本转到第 16 行的第一个空列,然后给我该特定单元格的行 + 列。所有代码都有效,但是,当它到达脚本的第 5 步时,它将“它有效”放在行的最后一个空列中,而不是第一个 .

​​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")
}

【问题讨论】:

  • 你说/* 5. Navigate to the first empty column of row */I would like for script to go to the first empty column of row 16还有is that it is placing "it worked" in the last empty column of the row, instead of the first.不知道是什么意思。来张照片吧。
  • 谢谢,我以为我附上了,现在有截图????

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


【解决方案1】:

试试这个:

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");
}

【讨论】:

  • AHHHHHHHH 成功了,成功了!非常感谢库珀!它现在将任何新数据放在该行的下一个空单元格中。好的,我现在要去谷歌去尝试并向自己解释为什么该代码有效,哈哈,倒数第二行代码是……不熟悉。非常感谢您在这方面的帮助!
  • reduce 方法非常有用。作为提示,考虑在这种情况下,累加器被初始化为一个对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-26
  • 2020-08-24
  • 2020-08-02
  • 1970-01-01
  • 2014-01-04
相关资源
最近更新 更多