Reputation: 125
I`m trying to add "Search Item" option in the tool bar. Basically, I have 6 different tabs in the same google sheet, but the # of items in each sheet is too many that users may not be able to find.
Is there way to add such tool in tool bar using Google sheet Script?
https://docs.google.com/spreadsheets/d/1lbfbMUhwKGG0CJ1tJuXVmwCRPAox_1dIATffhKVjkQM/edit?usp=sharing
Above is the link to the sample data
The data is generated from API for ad server system
In my real data, that "Creative" column has about > 1000 unique entries
Is there potentially, way to put option in a toolbar that does the "creative" search job, that can tell us creative "c6" was found in "first" tab, second row..
But search those items in only the "unhidden" tab
I`m not sure where I should start this with.. given the multiple tabs
Can someone please provide me suggestions?
Upvotes: 0
Views: 2057
Reputation: 201388
You want to show the result as follows.
Creative "C6" was found in "Second", "fifth" tab
Second Tab: Impression:100 / Click:100
Fifth Tab: Impression:100 / Click:100
I understood like above. If my understanding is correct, how about this sample script? Please think of this as just one of them.
Before you run the script, please set the sheet name to sheets
.
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Custom Menu')
.addItem('Show prompt', 'showPrompt')
.addToUi();
}
function showPrompt() {
// Please set the sheet name you want to use for searching.
var sheets = ["first", "second", "third", "fourth", "fifth", "sixth"];
var ui = SpreadsheetApp.getUi();
var res = ui.prompt('Sample', 'Input search text:', ui.ButtonSet.OK_CANCEL);
var button = res.getSelectedButton();
if (button == ui.Button.OK) {
var findText = res.getResponseText();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var header = "Creative \"" + findText + "\" was found in ";
var values = "";
var object = sheets.reduce(function(obj, sheetName, i) {
var sheet = ss.getSheetByName(sheetName);
var textFinder = sheet.createTextFinder(findText);
var searchedRows = textFinder.findAll().reduce(function(ar, e) {
if (e.getColumn() == 1) {
var row = sheet.getRange(e.getRow(), 1, 1, sheet.getLastColumn()).getValues()[0];
if (row[0] === findText) {
ar.push(row);
header += '"' + sheetName + '"' + (i == sheets.length - 1 ? " tab\n" : ", ");
values += sheetName + " Tab: Impression:" + row[1] + " / Click:" + row[2] + "\n";
}
}
return ar;
}, []);
if (searchedRows.length > 0) obj[sheetName] = searchedRows;
return obj;
}, {});
Logger.log(object);
ui.alert(header + values);
}
}
Input:
Output:
In order to reflect your above request to the script, please modify above script as follows.
ui.alert(header + values);
if (Object.keys(object).length > 0) {
ui.alert(header.slice(0, -2) + " tab\n" + values);
} else {
ui.alert("The creative you just searched for does not exist, please go back and check if there is any typo!");
}
Upvotes: 1