本文介绍了TypeError:在对象中找不到函数getDataRange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个脚本,该脚本将在整个工作簿"A"和工作簿"B"中搜索重复值,然后从其在工作簿"A"中的位置删除包含重复项的行.

I am trying to create a script that will search the entire of workbook "A" and workbook "B" for a duplicate value, and then delete the row containing the duplicate from its location in workbook "A".

但是,在运行脚本时,我又遇到错误"TypeError:在对象中找不到函数getDataRange",我也找不到原因.

However when running the script I get error "TypeError: Cannot find function getDataRange in object" back at me and I cannot figure out why.

我正在处理的脚本如下:

The script I am working on is below:

   function deleteRowInSpreadsheet1() { 
var s1 =SpreadsheetApp.openById("1pxWr3jOYZbcwlFR7igpSWa9BKCa2tTeliE8FwFCRTcQ").getSheets();
var s2 = SpreadsheetApp.openById("18jLxZlge_UFkdTjlO6opuDk1VKeezhEPLe9B7n1OfQs").getSheets(); 
var values1 = s1.getDataRange().getValues();
var values2 = s2.getDataRange().getValues();
var resultArray = [];
for(var n in values2){
var keep = true
for(var p in values1){
  if( values1[n][0] == values1[p][0] && values2[n][1] == values1[p][1]){
    keep=false ; break ;
  }
}
if(keep){ resultArray.push(values2[n])};
}
s1.clear()
s1.getRange(1,1,resultArray.length,resultArray[0].length).setValues(resultArray);
}

推荐答案

您正在调用getSheets(),它将返回一个工作表数组.因此,您需要遍历每个工作表,然后调用getDataRange().

You're calling getSheets(), which returns an Array of sheets. So you'll need to loop through each sheet and then call getDataRange().

例如:

var s1 =SpreadsheetApp.openById("1pxWr3jOYZbcwlFR7igpSWa9BKCa2tTeliE8FwFCRTcQ").getSheets();
for (var i=0; i<s1.length; i++) {
  var values = s1[i].getDataRange().getValues();
}

然后,当然,在需要的地方使用这些值.

Then, of course, use those values where you need them.

这篇关于TypeError:在对象中找不到函数getDataRange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-09 22:37