问题
I made a simple sheet based database for chemicals using google sheets. The sheet looks like this: https://docs.google.com/spreadsheets/d/e/2PACX-1vR0-AMEKNM3ZbDq67OIKWnc7E3KP8kfOsnr0Bjg2OSjpevLLjniknGXfIiiyZvbwE9bz3EfbOpO46ef/pubhtml?gid=292509613&single=true
There are many rows and columns. A user can change a value of a cell using a url link something like this https://docs.google.com/spreadsheets/d/13sLioJr_T6lqQ7y_pStBR8CKxReYwLUn4hao/edit#gid=292509613&range=B2
the link is converted to a QR code, which is used by someone who need to remotely change the value of the cell from phone, without searching for a specific chemical manually
The problems start when someone introduces a new row, and then location of each cell is changed, now all the url (i.e printed QR codes) select wrong chemicals. Now all the urls are wrong, and I have to manually fix this and move the new row to the bottom of the table
Ideally I would want select a cell with a query based on the chemical's ID (in this case cp1000) something like (in pseudocode) docs.google.com/spreadsheets/d/13sLioJr..../edit#gid=292509613&range=(select cell B(row_number) where cell A contains(cp1000))
An example of sucessful outcome will be selection of cell in column B based on column A
in pseudocode:
cell = Cell()
id = query.exctract_id()
for n in range(1,max_row):
if cell(column_number = 'A', row_number = n).value == id:
select cell(column_number ='B', row_number = n)
break
is there anyway to write it in a URL? Big thanks in advance
回答1:
Thanks for you suggestion. I indeed ended up by making a simple 1 page webapp. JS is actually surprisingly easy to learn. I use somewhat a weird way to pass variables from doGet() function, by storing them in a spreadsheet, since can return only html output with the function.
function doGet(e){
var url = "https://docs.google.com/spreadsheets/d/13sLioJr_T6lRODrvGSqQ7y_pStBR8CKxReYwLUn4hao/edit#gid=0";
var [ID, chemical, totalAmount, name, targetRow, unit] = fill(e);
var ss = SpreadsheetApp.openByUrl(url).getSheetByName("Supplementary");
ss.getRange("B5").setValue(ID);
ss.getRange("B6").setValue(name);
ss.getRange("B7").setValue(targetRow);
ss.getRange("B8").setValue(Session.getActiveUser().getEmail());
Logger.log(ID);
var data = {ID:ID, chemical :chemical, totalAmount :totalAmount, unit :unit};
var html = HtmlService
.createTemplateFromFile('FrontEnd.html')
.evaluate()
.getContent();
return HtmlService
.createTemplate(html +
"<script>\n" +
"postChemicals( " +
JSON.stringify(data) +
");\n</script>")
.evaluate();
}
//
function fill(e){
// this function extracts event parameters and gets a cell from spreadsheet with query from URL
// where name equals to spreadsheet name and ID to the chemical ID generated by "Code.gs"
var url = "<post your url>";
var ID = e.parameter.ID;
var name = e.parameter.name;
var targetRow =getCell(ID,name);
var sheet = SpreadsheetApp.openByUrl(url).getSheetByName(name);
var chemical = sheet.getRange("B"+targetRow).getValue()
var totalAmount = sheet.getRange("D"+targetRow).getValue()-
sheet.getRange("C"+targetRow).getValue();
var takenCell = sheet.getRange("C"+targetRow);
var unit = sheet.getRange("E"+targetRow).getValue();
Logger.log("fill(e) called");
return [ID, chemical, totalAmount,name, targetRow, unit ]
}
function getCell(chemNo, name){
var url = "https://docs.google.com/spreadsheets/d/13sLioJr_T6lRODrvGSqQ7y_pStBR8CKxReYwLUn4hao/edit#gid=0";
// this is a function that searches for a cell
//get the first sheet of your
var sheet = SpreadsheetApp.openByUrl(url).getSheetByName(name);
var lastRow = sheet.getLastRow();
var range = sheet.getRange('A1:A' + lastRow).getValues();
for(var i = 0; i < lastRow; i++){
if(range[i] == chemNo){
return (i + 1);
}
}
回答2:
Answer:
You can build a web-app which redirects to the cell you need based on the outcome of a conditional statement.
Details:
You can set each of the QR codes to contain the chemical ID as a URL parameter and then load the sheet based on what the parameter is:
Example Web-App URL:
https://script.google.com/a/google.com/macros/s/<web-app-ID>/exec?chemNo=cp1000
Apps Script code:
function doGet(e){
var sheetUrl = "<your-spreadsheet-URL-with-gid>";
var cell = getCell(e.parameter.chemNo, url);
return HtmlService.createHtmlOutput('<script>window.location.replace("' + sheetUrl + '&range=' + cell + '"</script>');
}
function getCell(chemNo, url){
//get the first sheet of your
var sheet = SpreadsheetApp.openByUrl(url).getSheets()[0];
var lastRow = sheet.getLastRow();
var range = sheet.getRange('A1:A' + lastRow).getValues();
for(var i = 0; i < lastRow; i++){
if(range[i] == chemNo){
return 'B' + (i + 1);
}
}
}
This will take the parameter, find which row it is in and return the cell BX where X is the corresponding row to which the chemNo is in, and finally redirect to the sheet with the range of the cell that you need.
来源:https://stackoverflow.com/questions/58208748/find-and-select-a-cell-based-on-url-in-google-sheets