简体   繁体   English

无法让 setValue 在 GoogleScripts 中正常工作

[英]Can't get setValue to work correctly in GoogleScripts

I'm very new to coding and I'm trying to create a simple button in Google Sheets that will take the data from the "input" tab and populate the last empty cells of the appropriate columns in the "feeding" tab.我对编码非常陌生,我正在尝试在 Google 表格中创建一个简单的按钮,该按钮将从“输入”选项卡中获取数据并填充“馈送”选项卡中相应列的最后一个空单元格。 I have it working to log the values of the feedings & date, but can't seem to get them to move.我可以记录喂食和日期的值,但似乎无法让它们移动。

Here's the code:这是代码:

//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 这是工作表副本的链接

The code in the question has at least two problems, getLastRow and setValues , on问题中的代码至少有两个问题, getLastRowsetValues ,关于

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 hasn't a setValues method. getLastRow没有setValues方法。
  • setValues requires and Array having an Array for each row, the inner Array, should have a value or Date object for each row's cell. setValues要求每个行都有一个数组,内部数组应该有一个值或 Date object 为每一行的单元格。 Since the variables used hold a single value / Date object, setValue should be used instead of setValues .由于使用的变量包含单个值/日期 object,因此应使用setValue而不是setValues

Related有关的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM