Reading a cell value using java or google script - javascript

I need a help reading cell value using either java or google script. The cell I am trying to read has a formula. When my script reads the data I get #DIV\0! error.
here is my simple script:
function readData(){
var Keys = "1Vp_fjFVFXHDjJRXl7C6mNzCK66jVB1I1BjieaZK6P";
var SheetName = "AAL";
var WR;
SS = SpreadsheetApp.openById(Keys);
Sheet = SS.getSheetByName(SheetName);
Range = Sheet.getDataRange();
Data = Range.getValues();
WR = Data[30][15];
}
Any help will be appriciated, Thanks.

Instead of doing [30][15], use this code to limit the range to one cell.
If that doesn't work, maybe your cell actually contains this DIV/0 value?
function readData(){
var Keys = "1Vp_fjFVFXHDjJRXl7C6mNzCK66jVB1I1BjieaZK6P";
var SheetName = "AAL";
var WR;
SS = SpreadsheetApp.openById(Keys);
Sheet = SS.getSheetByName(SheetName);
Range = Sheet.getRange(30, 15); //changed here
Data = Range.getValue(); //and here
WR = Data; //and here
}

Related

Remove parts of value by comma | Google Apps Script

I would like to remove the values ​​after the first comma. It is possible that there will be more values ​​and commas.
Expected result:
8-1
10-1
2-5
5-8
ss from Logger.log for var Id
function myFunction() {
var Sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var FirstRow = 7;
var LastRow = Sheet.getLastRow();
var RowRange = LastRow - FirstRow + 1;
var WholeRange = Sheet.getRange(FirstRow,3,RowRange,9);
var AllValues = WholeRange.getValues();
for (var i=0;i<AllValues.length;i++){
var CurrentRow = AllValues[i];
var Id = CurrentRow[0]; //col with ID Sheet1
var firstId = Id.map(vA=>[vA.split(',')[1]]);
}
}
Explanation:
You can use the split method and map to apply this operation for all the input data and get the first [0] element of the resulting array:
data.map(vA=>[vA.split(',')[0]])
Code snippet:
function myFunction() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet1');
const avals = sh.getRange('A1:A4').getValues().flat();
const bvals = avals.map(vA=>[vA.split(',')[0]]);
sh.getRange(1,2,bvals.length,1).setValues(bvals);
}
Sheet used for code snippet:
Make sure you format the input text as plain text, otherwise google sheets consider the input data to be dates or alternatively use getDisplayValues() instead of getValues() but I guess you have already figured out that part because console.log returns the correct input.
Updated answer based on your edited question:
Modify your current for loop as follows:
for (var i=0;i<AllValues.length;i++){
var CurrentRow = AllValues[i];
var Id = CurrentRow[0]; //col with ID Sheet1
var newId = Id.split(',')[0];
console.log(newId);
}

drive.properties for loop with google sheets

I have two functions in a google sheet that are meant to loop through a single column containing the file IDs of files in google drive, issuing a Properties.get to retrieve a single property "LastReview" for each document and paste all of the LastReview times in the next available column.
I'm having trouble getting the loop in "loopForMetadata" to work. I want it to acquire a list of all the LastReview times associated with each fileID and then post that to the next available column so that all of the LastReview times align with the fileIDs.
function getProperty(fileId) {
var propertyKey = 'LastReview'
var fileId = '1UaQkJU8r1kE9sxpFg6OD8aOuUCoRnSpB9Agg_R9HJ3s'
var response = JSON.stringify(Drive.Properties.get(fileId, 'LastReview', { visibility: 'PUBLIC' }).value);
var key = "value";
var resposeNoQuote = response.replace(/\"/g, "")
Logger.log(resposeNoQuote);
}
function loopForMetadata() {
var columnNeeded, data, lastColumn, sh;
sh = SpreadsheetApp.getActiveSheet();
lastColumn = sh.getLastColumn();
data = sh.getRange(1, 1, 1, lastColumn).getValues();//Get 2D array of all values in row one
data = data[0];//Get the first and only inner array
columnNeeded = data.indexOf('ID') + 1;//Arrays are zero indexed- add 1
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var rangeData = sheet.getDataRange();
var lastColumn = rangeData.getLastColumn();
var lastRow = rangeData.getLastRow();
var searchRange = sheet.getRange(2, columnNeeded, lastRow - 1, 1);
// Get array of values in the search Range
var rangeValues = searchRange.getValues();
// Loop through array and if condition met, add relevant
// background color.
var resultValues = []
for (i = 0; i < rangeValues.length; i++) {
resultValues.push(getProperty(rangeValues[i]));
Utilities.sleep(10);
}
Logger.log(resultValues);
};
I believe your goal as situation as follows.
You want to retrieve the values using the key of LastReview in the property from the files of fileId.
You want to put the retrieved values to the same row of fileId in the next available column.
You want to achieve this using Google Apps Script.
Modification point:
In your script,
getProperty() doesn't return the values.
resultValues is not put to the Spreadsheet.
When you want to retrieve the value of the key LastReview, Drive.Properties.get(fileId, 'LastReview', { visibility: 'PUBLIC' }).value directly returns the value.
As an important point, when the file of fileId has not property of the key LastReview, it seems that Drive.Properties.get() occurs an error. So in this modification, as a simple workaround, I used the try catch.
When above points are reflected to your script, it becomes as follows.
Sample script:
function loopForMetadata() {
var columnNeeded, data, lastColumn, sh;
sh = SpreadsheetApp.getActiveSheet();
lastColumn = sh.getLastColumn();
data = sh.getRange(1, 1, 1, lastColumn).getValues();//Get 2D array of all values in row one
data = data[0];//Get the first and only inner array
columnNeeded = data.indexOf('ID') + 1;//Arrays are zero indexed- add 1
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var rangeData = sheet.getDataRange();
var lastColumn = rangeData.getLastColumn();
var lastRow = rangeData.getLastRow();
var searchRange = sheet.getRange(2, columnNeeded, lastRow - 1, 1);
var rangeValues = searchRange.getValues();
var resultValues = []
// I modified below script.
for (i = 0; i < rangeValues.length; i++) {
var fileId = rangeValues[i];
var value = "";
try {
value = Drive.Properties.get(fileId, 'LastReview', { visibility: 'PUBLIC' }).value;
} catch(e) {}
resultValues.push([value]);
// Utilities.sleep(10); // I'm not sure whether this is required.
}
sheet.getRange(2, lastColumn + 1, resultValues.length, 1).setValues(resultValues);
Logger.log(resultValues);
};
Note:
Please confirm whether Drive API is enabled at Advanced Google services, again.
Reference:
Properties: get

Copy Google Sheet Data to other Google Sheet below the Last Row

I am trying to copy one Google Sheet Data to Other Google Sheet after first available free row.
function CopyDataToNewFile() {
var sss = SpreadsheetApp.openById('1vHc........'); // sss = source spreadsheet
var ss = sss.getSheetByName('Database'); // ss = source sheet
//Get full range of data
var SRange = ss.getDataRange();
//get A1 notation identifying the range
var A1Range = SRange.getA1Notation();
//get the data values in range
var SData = SRange.getValues();
var tss = SpreadsheetApp.openById('1ss......'); // tss = target spreadsheet
var ts = tss.getSheetByName('CalDatabase'); // ts = target sheet
ts.getRange(A1Range).setValues(SData);
}
Above quote works fine but always Copy into same Row, but I want it to copy the data into a new blank row available below.
Try this -
function CopyDataToNewFile() {
var sss = SpreadsheetApp.openById('1vHc........'); // sss = source spreadsheet
var ss = sss.getSheetByName('Database'); // ss = source sheet
var SData = ss.getDataRange().getValues();
var tss = SpreadsheetApp.openById('1ss......'); // tss = target spreadsheet
var ts = tss.getSheetByName('CalDatabase'); // ts = target sheet
ts.getRange(ts.getLastRow()+1,1,SData.length,SData[0].length).setValues(SData);
}

Getting the value in float even though input value is integer

I am reading the data from Google spread sheet using Google app script.
in one of the cell(sprint version) of spreadsheet I have mentioned the value as 56 but when I am reading the value it is returning 56.0. I don't know why
Below method, I wrote to read the value from spreadsheet
function readAndSetTimePeriodMetaData() {
var timePeriodArray = new Array();
var masterSheetID = "My Google Sheet ID";
var masterSheet = SpreadsheetApp.openById(masterSheetID);
var timePeriodDataSheet = masterSheet.getSheetByName("Time Period MetaData");
data = timePeriodDataSheet.getDataRange().getValues();
var range = timePeriodDataSheet.getRange(1,2);
var sprintVersion = range.getValue(); //it is returning 56.0
var range = timePeriodDataSheet.getRange(2,2);
var monthValue = range.getValue();
var range = timePeriodDataSheet.getRange(3,2);
var quarterValue = range.getValue();
Logger.log(sprintVersion)
}
I solved it by using toFixed(0)
var sprintVersion = range.getValue().toFixed(0);

Cannot find method getRange(number,number,number,number)

In an attempt to stream line some processes I have attempted to create a script to move a paper process into a google form. I've nhever really tinkered with Javascript but I have been following the trail of errors down to this one which I cannot seem to shake.
Currently I'm trying to define the range of data but keep getting the error mentioned in the title. Am I incorrectly calling the sheet in the script? I cannot seem to figure out how to properly define them.
Below is the section that defines the variables and sheets. Anything I'm doing incorrectly?
function sendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
Logger.log(ss.getName());
var sheet = ss.getActiveSheet()[0];
var startRow = 2;
// First row of data to process
var numRows = 12;
// Number of rows to process
var dataRange = ss.getRange(startRow, 2, numRows, 12)
//Assigning spreadsheet feilds
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var firstName = row[1];
var guestFirstN = row[6];
var guestLastN = row[7];
var arrivalDate = row[8];
var numberNights = row[9];
var rmName = row[10];
var rmAgree = row[11];
You first need to define the Sheet you want to get the data from since a Spreadsheet can have multiple Sheets.
Try replacing
var dataRange = ss.getRange(startRow, 2, numRows, 12)
with
var dataRange = ss.getActiveSheet().getRange(startRow, 2, numRows, 12);

Categories