我正在尝试编写一个代码,该代码将在sheet1中查看A3的值,并检查它是否存在于sheet2的A列中。
发生的事情是它只在有相应的值时起作用。否则我有这个" TypeError:无法读取属性" 0"来自undefined。 (第14行,文件" testing2")。
请帮忙!
function findInColumn(column, id) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var columntosearch = ss.getRange('TABELA DE HISTORICO DE PAGAMENTOS!A:A');
var id = ss.getRange('TABELA CALCULADORA RETENCAO IMPOSTOSINDIRETOS!A3').getValue();
var values = columntosearch.getValues();
var row = 0;
while ( values[row] && values[row][0] !== id ) {
row++;
}
if (values[row][0] === id)
ss.getRange('TABELA CALCULADORA RETENCAO IMPOSTOS INDIRETOS!A3').copyTo(ss.getRange('TABELA CALCULADORA RETENCAO IMPOSTOS INDIRETOS!A6'), {contentsOnly:true});
else
ss.getRange('TABELA CALCULADORA RETENCAO IMPOSTOS INDIRETOS!D3').copyTo(ss.getRange('TABELA CALCULADORA RETENCAO IMPOSTOS INDIRETOS!D6'), {contentsOnly:true});
}
答案 0 :(得分:0)
function valueOfA3insheet1inColumnAsheet2()
{
var ss=SpreadsheetApp.getActive();
var sht1=ss.getSheetByName('sheet1');
var rng1=sht1.getRange('A3');
var valueinA3 = rng1.getValue();
var sht2=ss.getSheetByName('sheet2');
var rng2=sht2.getRange('A:A');
var rng2A=rng2.getValues();
var s='<table border="1"><tr><th>Value</th><th>Cell</th></tr>';
for(var i=0;i<rng2A.length;i++)
{
if(rng2A[i][0]==valueinA3)
{
var r = sht2.getRange(i+1,1,1,1).getA1Notation();
s+='<tr><td>' + rng2A[i][0] + '</td><td>' + r + '</td></tr>'
}
}
s+='</table>';
var html=HtmlService.createHtmlOutput(s);
SpreadsheetApp.getUi().showModelessDialog(html, 'Values Found')
}
答案 1 :(得分:0)
我认为Javascript方法indexOf会帮助你。以下是文档:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf?v=control
我已经更改了一些代码来帮助你。内联评论可以解释这些变化。
function findInColumn(column, id) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
//getValues returns a multidimensionalarray, so i am using reduce to flatten it
var columntosearch = ss.getRange('TABELA DE HISTORICO DE PAGAMENTOS!A:A')
.getValues()
.reduce(function(a, b) {return a.concat(b);});
var id = ss.getRange('TABELA CALCULADORA RETENCAO IMPOSTOSINDIRETOS!A3').getValue();
//indexOf returns the index where the id is found or -1 if it isn't found
var isInColumn = columntosearch.indexOf(id);
//check that isInColumn is not -1
if(isInColumn > -1)
ss.getRange('TABELA CALCULADORA RETENCAO IMPOSTOS INDIRETOS!A3').copyTo(ss.getRange('TABELA CALCULADORA RETENCAO IMPOSTOS INDIRETOS!A6'), {contentsOnly:true});
else
ss.getRange('TABELA CALCULADORA RETENCAO IMPOSTOS INDIRETOS!D3').copyTo(ss.getRange('TABELA CALCULADORA RETENCAO IMPOSTOS INDIRETOS!D6'), {contentsOnly:true});
}
希望有所帮助!