所以我希望我的程序在我的谷歌表格的第一列中搜索一个元素,如果找到该元素,则返回相应的行。当我测试此代码时,即使该元素存在,也显示它不存在。function doGet(e){ var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1AssaPoFPcazneN44EjE04fE99MxTaPZg5uMsO1Klh4o/edit#gid=0"); var sheet = ss.getSheetByName("Sheet1"); return finds(e,sheet);}function doPost(e){ var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1AssaPoFPcazneN44EjE04fE99MxTaPZg5uMsO1Klh4o/edit#gid=0"); var sheet = ss.getSheetByName("Sheet1"); return finds(e,sheet); }function finds(e,sheet) { var records = {}; var scannedData = e.parameter.sdata; var range = sheet.getDataRange().getDisplayValues().map(r => r[0]); // -> get display Data as 1D array - Col A var data = []; if(range.includes(scannedData)) { // use includes to see if scannedData is included in the array var row_scannedData = range.indexOf(scannedData); var result = sheet.getRange(row_scannedData,1,1,4); //fetches the entire row belonging to the result data.push(result); //pushes the row in an array records.items = data; var finalresult = JSON.stringify(records); return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.JSON); } return ContentService.createTextOutput("Not Found").setMimeType(ContentService.MimeType.TEXT);}
1 回答
慕婉清6462132
TA贡献1804条经验 获得超2个赞
回答:
您可以使用TextFinder
.
代码示例:
假设 A 列中只有一个单元格包含您的搜索词:
function searchColumnA(searchTerm) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var aColumn = sheet.getRange("A1:A" + sheeet.getLastRow());
var tf = aColumn.createTextFinder(searchTerm).findAll();
return tf[0].getRow();
}
添加回答
举报
0/150
提交
取消