I'm trying to build a data validation for a cell from a range in a different sheet. I've tried the below code, and I continue to get this error "". Any help would be greatly appreciated.
function dataValidations(){
var builder = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Basic Info");
var invMgmt = SpreadsheetApp.openById("1eGNsJ2iB_IOzcfJe7DyFV_0WhzdQhvW2Lqdzfta2eV4");
var partners = invMgmt.getSheetByName("Partners and Locations").getRange(2,1,75,1).getValues();
var partnerRule = SpreadsheetApp.newDataValidation().requireValueInRange(partners).build();
var origin = builder.getRange("B4");
origin.setDataValidation(partnerRule);
}
In the case of requireValueInRange(range), range is the range object. In your script, the values retrieved by getValues() is 2 dimensional array. I think that this might be the reason of your issue. So in order to remove this issue, how about the following modification?
From:
var partners = invMgmt.getSheetByName("Partners and Locations").getRange(2,1,75,1).getValues();
var partnerRule = SpreadsheetApp.newDataValidation().requireValueInRange(partners).build();
To:
var partners = invMgmt.getSheetByName("Partners and Locations").getRange(2,1,75,1).getValues();
var partnerRule = SpreadsheetApp.newDataValidation().requireValueInList(partners.flat()).build(); // Modified
Note:
When SpreadsheetApp.openById("###").getSheetByName("###").getRange(###) is used for requireValueInRange, no error occurs and the retrieved values are the correct from other Spreadsheet. But when I confirmed the data validation in the cell, it is found that SpreadsheetApp.getActiveSpreadsheet() is used instead of SpreadsheetApp.openById("###"). So in this case, it seems that using requireValueInList is suitable.
References:
requireValueInRange(range)
requireValueInList(values)
flat()
Related
I am writing what should be a simple HTML form for users to make a reservation. However, having difficulty doing something I've done many times before - getting data from a GSheet and presenting it in an HTML form. Code snippets:
**Server side**
//Getting data from spreadsheet; expecting it to be an array of arrays.
function getMembers(){
const url = "https://my gsheet url"
const ss = SpreadsheetApp.openByUrl(url);
const ws = ss.getSheetByName("Members");
const row = ws.getLastRow()
const memberData = ws.getRange(2,1,row-1,7).getValues();
const array = JSON.stringify(memberData); //EDIT...helps get data to client side
return //array; //this data to be passed to browser when called. Type Error?
//Log(memberData) = //Looks like an array to me...
//[1,"Simmie","Smith","Simmie Smith","2020-01-01T05:00:00.000Z","temp","53120-1"],
//[2,"Fred","Williams","Fred Williams","2020-01-01T05:00:00.000Z","temp","53120-2"],
//[3,"Carleton","Johnson","Carleton Johnson","2020-01-01T05:00:00.000Z","temp","53120-3"],
//[4,"Caroll","Williams","Caroll Williams","2020-01-01T05:00:00.000Z","temp","53120-4"],
//[5,"Gabby","Williams","Gabby Williams","2020-01-01T05:00:00.000Z","temp","53120-5"]
//EDIT HtmlService code
function doGet(e){
return HtmlService.createTemplateFromFile('Reservations HTML')
.evaluate().setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
Calling the above server function when page loads so that member name can be presented in a select dropdown and other info from the array used elsewhere.
**Browser JS**
function getMemberData(){
google.script.run.withSuccessHandler(populateMembersDD).getMembers();
}
function populateMembersDD(data){
console.log(data) //undefined - same result FFox or Chrome..
memberInfo = data.filter(function(row){return true});
not getting any further than that because data is not passing to browser in a format with which I can apply necessary methods to get data into dropdown. Have tried various methods (slice, filter etc) to convert data into an array but none have worked. I suspect a type error, as though server function is not producing an object in array format. But I cannot figure out why. Have read many similar questions/answers on Stack but none have solved my problem. Help much appreciated as I am 24 hours in on solution experiments. Have many other apps where this basic code pattern works totally fine...
Very new to this, but have been pretty lucky with my tinkering in the past. Really stuck on this one, however.
Looking to import file data to specific sheets. With a specific file name, I've been successful with this script:
function importVauto() {
var app = SpreadsheetApp;
var import1 = app.getActiveSpreadsheet().getSheetByName('Import1');
var file = DriveApp.getFilesByName("samplefilename").next();
var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
import1.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
The challenge is, the file names change weekly. My thought was to have the required file names listed in a spreadsheet, reference the cell containing the file name, and import the required file. Tried this:
function importVauto() {
var app = SpreadsheetApp;
var import1 = app.getActiveSpreadsheet().getSheetByName('Import1');
var data = app.getActiveSpreadsheet().getSheetByName('Data');
var name = data.getRange("C24")
name.getValues()
Logger.log(name)
var file = DriveApp.getFilesByName(name).next();
var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
import1.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
Appears there is a problem with using the var 'name' as the file name for DriveApp.getFilesByName(), but chances are, there's a ton I'm missing (don't know what you don't know).
Hopefully this question makes sense (and even more hopefully, there is a simple solution). Again, very new to this. Appreciate any feedback.
How about this modification?
Modification points :
getValues() returns 2 dimensional array. If you want to retrieve the value of one cell "C24", you can use getValue(). In this case, you can directly retrieve the string value of the cell.
Modified script :
From :
name.getValues();
To :
name = name.getValue();
OR
name = name.getValues()[0][0];
Note :
When you retrieve file using DriveApp.getFilesByName(name).next(); if there are several files with the same filename, this can retrieve only one of them. Please be careful about this.
References :
getValues()
getValue()
If this was not what you want, please tell me. I would like to modify my answer.
I have a script that pulls in data from different endpoints from an API.
The script then puts the data in a google sheet. In this sheet I want to chart the data or perform a math/sum operation. I am able to do this with the data from one endpoint just fine. But the other endpoints (using the same code) does not allow me to do this. I can do =A1+A2 and the value will add. But if I try to do =sum(A1:A2) the result is 0. No matter what I do. I am able to do the sum() operation on the other endpoint. Its very peculiar.
Here is some of the code from my apps script:
var response = UrlFetchApp.fetch(requestUrl, params);
var json = JSON.parse(response.getContentText());
var rows = [], jsondata;
for (i = 0; i < json.length; i++) {
jsondata = json[i];
rows.push([jsondata.currency, jsondata.amount, jsondata.fee, jsondata.date]);
}
return rows;
}
Your problem is with number formats, change the cells by highlighting them go to 'Format' -> 'Number' -> 'Automatic' and then it will work with =SUM(A1:A2)
To fix this issue in your script, use .setNumberFormat('0') on the range to set the format you require. The number format may need to be '0.00' if you are working with decimals.
So I'm new to all of this, both BigQuery and AppScript (coding in general..) and I'm learning as I go, so maybe to some my question may seem stupid. Please just hear me out.
I have created a script that loads 10 of the most recent data points into a Google Sheets doc from one of my BigQuery tables. Now, when I manually add new data points to the bottom of this table, I would like to have a load script run that uploads that new data back into BigQuery, and appends it to my original table. I read somewhere that just by inserting a new table the data is automatically appended if the table mentioned already exists. However, I haven't tested that part yet since I get stuck on an error earlier up the line..
Below is the load script I have loosely copied from https://developers.google.com/apps-script/advanced/bigquery
function loadCsv() {
var projectId = 'XX';
var datasetId = 'YY';
var tableId = 'daily_stats_testing';
var file = SpreadsheetApp.getActiveSpreadsheet();
var sheet = file.getActiveSheet().getRange("A12:AK10000").getValues();
var data = sheet.getBlob().setContentType('application/octet-stream');
var job = {
configuration: {
load: {
destinationTable: {
projectId: projectId,
datasetId: datasetId,
tableId: tableId
},
skipLeadingRows: 1
}
}
};
job = BigQuery.Jobs.insert(job, projectId, data);
var Msg = "Load started. Check status of it here:" +
"https://bigquery.cloud.google.com/jobs/%s", projectId
Logger.log(Msg);
Browser.msgBox(Msg);
return;
}
Now the error I get (in a variety of forms, since I've been testing stuff out) is that the BigQuery.Jobs function only accepts Blob data, and that the data from my current sheet (with rage A12 marking the first manually imputed row of data) is not a Blob recognizable data set.
Is there any way (any function?) I can use that will directly convert the selected data range and make it Blob compatible?
Does anyone have any recommendation on how to do this more efficiently?
Unfortunately the script has to load directly out of the current, open Spreadsheet sheet, since it is part of a larger script I will be running. Hopefully this isn't too much of a hinder!
Thanks in advance for the help :)
Is there any way (any function?) I can use that will directly convert the selected data range and make it Blob compatible?
There is actually a function that does convert objects into blob type, namely newBlob(data).
To test this I got a range from my spreadsheet and used it.
function blobTest(){
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:C1");
Logger.log(range); //here, range data is of type Range object
var blob = Utilities.newBlob(range);
Logger.log(blob); //here, range data is now of type Blob object
}
In Google Sheets, which runs on JavaScript, I'm getting this message, "Cannot find method setActiveSheet(string). (line 4, file "Code")", I don't know why... I'm pretty new to coding, so bear with me.
function onOpen() {
var email = Session.getActiveUser().getEmail();
var sheet = email.slice(0,-11);
SpreadsheetApp.setActiveSheet(sheet)
}
You're getting the "Cannot find method" error because the setActiveSheet() method takes an argument of type Sheet, not a string. See the specification here: https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app#setActiveSheet(Sheet)
In order to get a Sheet object from the string, you need to open the parent Spreadsheet, then get the appropriate sheet by name, then you can pass it to setActiveSheet.
Assuming this script is embedded in the relevant Spreadsheet, that looks like this:
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(spreadsheet.getSheetByName(sheet));
This worked well for me.
function theSecondSheet() {
var activeSpreadSheet = SpreadsheetApp.getActiveSpreadsheet();
if(activeSpreadSheet.getSheetByName("Sheet2").activate()){
SpreadsheetApp.setActiveSheet(activeSpreadSheet.getSheetByName("Sheet2"));
}
}