In order to obtain the necessary code to get a function that can be executed every time a local is selected in "id-local" and that what it does is the following: with the data that this input has
<form class="row g-12 " id="facturacion-form">
<div class="col-md-3 input-group-lg has-validation">
<label for="id-local" class="form-label">LOCAL</label>
<select class="form-select" id="id-local" name="id-local" required>
<option selected>Elije local</option> <?!=options?>
</select>
<div class="invalid-feedback"> Por favor elige un local
</div>
</div>
look in the google sheets sheet: "XXXXXXXXX", in the sheet with name "DB_LOCALES" in the range "B:O" where in "B" are the name of the premises and in "O" are the name of the companies corresponding, so that in this other input
<div class="input-group ">
<span class="input-group-text">Sociedad</span>
<input type="text" class="form-control" id="sociedad-local" placeholder="Cooland Proyect S.L" aria-label="sociedad-local" aria-describedby="sociedad-local">
</div>
At the moment I have the following function:
function updateSociedad() {
var libroLocales = SpreadsheetApp.openById("XXXXXXXXXXX");
var hojaLocales = libroLocales.getSheetByName("DB_LOCALES");
var rangesoc = hojaLocales.getRange("B:O");
var values = rangesoc.getValues();
var local = document.getElementById("id-local").value;
for (var i = 0; i < values.length; i++) {
if (values[i][0] == local) {
// Actualizar el valor del campo "sociedad"
var sociedad = values[i][14]; // Columna "O"
document.getElementById("sociedad-local").textContent = sociedad;
break;
}
console.log(values);
}
}
but if I put it in the .gs it gives me errors that it can’t access the document.getElementById but if I put it in the HTML it shows me that it can’t access the spreadsheet
My intention was to create a call to the function from another input, but it is being impossible due to my lack of experience.
I would greatly appreciate help
2
Answers
I believe your goal is as follows.
<select class="form-select" id="id-local" name="id-local" required>
, you want to search a value from the column "B" ofhojaLocales.getRange("B:O")
using the selected value. And, when the value was found, you want to put the value of column "O" of the same row into<input type="text" class="form-control" id="sociedad-local" placeholder="Cooland Proyect S.L" aria-label="sociedad-local" aria-describedby="sociedad-local">
.Modification points:
<?!=options?>
. So, in this modification, it supposes that the script for giving<?!=options?>
are valid values for searching withhojaLocales.getRange("B:O")
. Please be careful about this.document.getElementById
.var rangesoc = hojaLocales.getRange("B:O");
. In this case, the values are columns "B" to "O". But, in your loop,var sociedad = values[i][14]; // Columna "O"
is used. In this case, the value is trying to be retrieved from column "P".input
tag,value
is used.When these points are reflected in your script, how about the following simple modification?
HTML & Javascript side:
Google Apps Script side:
In this case, in order to search for value from column "B", TextFinder is used.
Testing:
When the above HTML is opened as the HTML template,
<?!=options?>
is loaded. And, when you select one of options, the functionsample
is run. And, the selected value is sent to Google Apps Script side withgoogle.script.run
. And, the value is searched from the column "B". And, when the value was found, the value of column "O" is returned. At Javascript side, the returned value is used inwithSuccessHandler
. And, the value is put into<input type="text" class="form-control" id="sociedad-local" placeholder="Cooland Proyect S.L" aria-label="sociedad-local" aria-describedby="sociedad-local">
.Note:
Unfortunately, I couldn’t know the method for opening the HTML from your question. If you are using Web Apps, please be careful about the following points.
When you modified the Google Apps Script of Web Apps, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful about this.
You can see the detail of this in my report "Redeploying Web Apps without Changing URL of Web Apps for new IDE (Author: me)".
Reference:
As previously indicated on the comments, implementation of the google.script.run class is necessary. To facilitate your project, we suggest referring to the following concise example (added a sample testing button on the html code to run the process):
Script
Code.gs
Index.html
DEMO
DB_LOCALES
sheet data:References