获取和设置单元格的值无效。

4

我在电子表格中获取单元格的值时遇到了麻烦。在文档中,我找到了getCell()方法,但它并不起作用。它总是只获得“范围”,而不是值(它是一个整数)。而且我找不到setCell()方法!值在单元格B:1中。谢谢任何帮助!

var API_KEY           = "***",
    PROFILE_ID        = "****";
    GET_PLUS_ONES_URI = "https://www.googleapis.com/plus/v1/people/"+PROFILE_ID+"?fields=plusOneCount&key="+API_KEY;
var currentPlusOnes = 0,
    lastPlusOnes    = 0,
    ss              = SpreadsheetApp.getActive();

function execute() {
  getCurrentPlusOnes();
  getLastPlusOnes();
  if ( currentPlusOnes !== getLastPlusOnes ) {
    setCurrentValue();
  }
}

function getCurrentPlusOnes() {
  currentPlusOnes = JSON.parse( UrlFetchApp.fetch( GET_PLUS_ONES_URI )).plusOneCount;
}

function getLastPlusOnes() {
  lastPlusOnes = ss.getRange("B1").getCell( 1, 1 ); // --> Allways just "Range" instead of the value
}

function setCurrentValue() {
  ss.getDataRange().setValue( currentPlusOnes );
}

3
lastPlusOnes = ss.getRange("B1").getValue(); - ScampMichael
2个回答

5

both getRange()getCell() 都会返回一个 Range 对象。您需要在范围上调用 getValue()getValues() 来获取数据。在这种情况下,由于您的第一个 getRange("B1") 只引用了一个单元格,因此不需要使用 getCell(1,1)

lastPlusOnes = ss.getRange("B1").getValue();

https://developers.google.com/apps-script/reference/spreadsheet/range#getValue()

对于设置相同内容,单个单元格中的单个值应为:

Range.setValue( number | string )

或者,如果您有一堆行和列,则首先选择适当大小的范围,然后使用setValues([][])

3

哦,好的。我的错。对不起 :( :D - phip1611

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接