Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

google apps script - Issue with setValues

I have this code :

function calculateTotals(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var total, clientID,cellValues,cellFontLines, result;

  for (i=0;i<98;i++) {
    //sheet.getRange("K"+i).setValue("calculating");
    clientID=sheet.getRange("J3:J100").getValues();
    total=0;
    cellValues=sheet.getRange("B3:H234").getValues();
    cellFontLines=sheet.getRange("B3:H234").getFontLines();
    result=sheet.getRange("K3:K100").getValues(); // this is dumb

    if (clientID[i][0] != "") {
      for (j=0;j<=6;j++) {
        for (k=0;k<=231;k++) {
          if (cellValues[k][j] == clientID[i][0] && cellFontLines[k][j] != 'line-through' ) {
            total++;
          }
        }
      }
      result[i][0]=total/2;
      Logger.log(i,clientID[i][0],result[i][0]);
      //sheet.getRange("K"+(i+3)).setValue(result[i][0]);
    } else {
      break;
    }
  }
  sheet.getRange("K3:K100").setValues(result); // this doesn't seem to do anything
   Logger.log(result);
}

I can't figure out how to use setValues. I logged it all, and it looks right, in the debugger the array is all set but I still have to use the commented out setValue() line to update that column.

As an aside, is there a better way to initialize result to the correct dimensions than just reading in the previous results?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This works :

function calculateTotals(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var total, clientID,cellValues,cellFontLines, result=new Array(98);

  clientID=sheet.getRange("J3:J100").getValues();
  cellValues=sheet.getRange("B3:H234").getValues();
  cellFontLines=sheet.getRange("B3:H234").getFontLines();
  result=sheet.getRange("K3:K100").getValues();

  for (i=0;i<98;i++) {
    //sheet.getRange("K"+i).setValue("calculating");
    total=0
    if (clientID[i][0] != "") {
      for (j=0;j<=6;j++) {
        for (k=0;k<=231;k++) {
          if (cellValues[k][j] == clientID[i][0] && cellFontLines[k][j] != 'line-through' ) {
            total++;
          }
        }
      }
      result[i][0]=total/2;
    } else {
      break;
    }
  }
    sheet.getRange("K3:K100").setValues(result);
  
}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...