【问题标题】:Can't get setValue to work correctly in GoogleScripts无法让 setValue 在 GoogleScripts 中正常工作
【发布时间】:2021-05-08 14:29:20
【问题描述】:

我对编码非常陌生,我正在尝试在 Google 表格中创建一个简单的按钮,该按钮将从“输入”选项卡中获取数据并填充“供稿”选项卡中相应列的最后一个空单元格.我可以记录喂食和日期的值,但似乎无法让它们移动。

代码如下:

//Gets data from 'Input' sheet
function recordData() {
  let src = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Input'); //Source
  let dst = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Feedings'); //Destination

  let input = {};
    input.milk = src.getRange('B2').getValue()
    input.formula = src.getRange('B3').getValue()
  let today = new Date();
    today.date = today.getMonth()+1+'/'+today.getDate();
    today.formatTime = function formatAMPM (time) {
    var hours = time.getHours();
    var minutes = time.getMinutes();
    var ampm = hours >= 12 ? 'pm' : 'am';
    hours = hours % 12;
    hours = hours ? hours : 12; // the hour '0' should be '12'
    minutes = minutes < 10 ? '0'+minutes : minutes;
    var endTime = hours + ":" + minutes + " "+ ampm;
    return endTime;
    }
    today.time = today.formatTime (new Date);

  Logger.log("formula = " + input.formula);
  Logger.log("milk = " + input.milk);
  Logger.log("date = "+ today.date);
  Logger.log("time = "+ today.time);


//moves data from 'Input' to 'Feeding' sheet. **NOT WORKING**
  function moveData () {
        var dstFeedings = {};
          dstFeedings.date = dst.getRange("Feedings!A2:A").getLastrow.setValues(today.date);
          dstFeedings.time = dst.getRange("Feedings!B2:B").getLastRow.setValues(today.time);
          dstFeedings.formula = dst.getRange("Feedings!C2:C").getLastRow.setValues(input.formula);
          dstFeedings.milk = dst.getRange("Feedings!D2:D").getLastRow.setValues(input.milk);
        return dstFeedings;
      }  
}

Here's the link to a copy of the sheet

【问题讨论】:

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


    【解决方案1】:

    问题中的代码至少有两个问题,getLastRowsetValues,on

    dstFeedings.date = dst.getRange("Feedings!A2:A").getLastrow.setValues(today.date);
    dstFeedings.time = dst.getRange("Feedings!B2:B").getLastRow.setValues(today.time);
    dstFeedings.formula = dst.getRange("Feedings!C2:C").getLastRow.setValues(input.formula);
    dstFeedings.milk = dst.getRange("Feedings!D2:D").getLastRow.setValues(input.milk);
    
    • getLastRow 没有 setValues 方法。
    • setValues 要求每个行都有一个 Array,内部 Array,每个行的单元格都应该有一个值或 Date 对象。由于使用的变量包含单个值/日期对象,因此应使用setValue 而不是setValues

    相关

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-05
      • 2017-11-06
      • 2011-09-06
      • 1970-01-01
      • 2023-03-10
      • 2020-10-16
      • 2014-01-15
      • 2019-07-29
      相关资源
      最近更新 更多