如何通过匹配其他[表格]来替换[单元格]?

时间:2013-12-27 16:59:29

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

我想将sheet1 col A与sheet2 colunm B匹配。

如果是sheet1!答:A = sheet2! B:B

用Sheet2替换Sheet1 C:C! d:d

我想在Google电子表格上这样做,但不太了解java。怎么做??

function replaceCCWithDD(){ // in this example sheet, the function will replace sheet1 C:C from Sheet2! D:D if Sheet1! A:A matched with Sheet2! B:B

var sh1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheet1');
var sh2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheet2');

1 个答案:

答案 0 :(得分:0)

尝试下面的代码:

function myFunction() {
var sh1 = SpreadsheetApp.openById("0AjCcE3eIeB3pdFNFTUdQN1M5Rm4wOU5SSzlXb2Utc2c").getSheetByName('sheet1');
var sh2 = SpreadsheetApp.openById("0AjCcE3eIeB3pdFNFTUdQN1M5Rm4wOU5SSzlXb2Utc2c").getSheetByName('sheet2');

var colA = sh1.getDataRange().getValues();
var colB = sh2.getDataRange().getValues();

for(var x in colA) {    
    Logger.log(x < colB.length && colA[x][0] == colB[x][1]);
    if(x < colB.length && colA[x][0] == colB[x][1]) {         
     colA[x][2] = colB[x][3];
    }
  }  
  sh1.getDataRange().setValues(colA);
}

实时版here