How to get values inside a range slider from an array - javascript

I can't get the array values (alphabet) linked to the slider. It gives a number instead of a letter.
Javascript only is the goal here btw.
I tried giving it a createSlider.value = allTheLetters among some other things but it is not working.
// Creating the Alphabet Array
var allTheLetters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
// Creating button
var button = document.createElement("button");
button.style.display = "block";
button.id = "sliderButton";
var sliderButtonId = document.getElementById("sliderButton");
button.innerHTML = "Add new letter";
// End creating Button
// Creating slider and displaying it to the DOM
var sliderParent = document.getElementById("target");
var createSlider = document.createElement("input");
createSlider.type = "range";
createSlider.min = 0;
createSlider.max = allTheLetters.length;
sliderParent.appendChild(createSlider);
sliderParent.appendChild(button);
// End slidercreation
the value of createSlider now is 50, i'd like it to start with the letter A and go max to the letter Z.

Turns out you can't get a non numeric value inside a slider, so you have to get the value of the slider position and make it link to the array you created like below:
// Creating slider and displaying it to the DOM
var sliderParent = document.getElementById("target");
var createSlider = document.createElement("input");
createSlider.type = "range";
createSlider.min = 0;
createSlider.max = allTheLetters.length-1;
sliderParent.appendChild(createSlider);
sliderParent.appendChild(button);
// get Value of slider location
var sliderResult = createSlider.value;
var theLetterIWant = allTheLetters[sliderResult];
console.log(theLetterIWant);
// End slidercreation

Related

Google Apps Script: Replace all text in slide table with values and formatting from sheets

Here is the description of my problem.
I have a range of placeholder text and its associated values in a spreadsheet. The placeholder text also exists in a slide presentation which gets replaced using the replacealltext function. However, the colors in the spreadsheet for the values do not change. Please see the examples below.
Google Apps Script:
// Google Sheets
var masterSheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = masterSheet.getSheetByName('Monthly Report'); //gets the data from the active pubfile
var range = sheet.getRange('S1:T110');
var data = range.getValues();
var titleName = sheet.getRange('AA14:AA14').getValues().toString();
data.shift(); //removes the headers in the sheets
// Creating the new slide
var spreadsheetID = SpreadsheetApp.getActiveSpreadsheet().getId(); //Ensure Code is applied to active Pubfile
var spreadsheetFolder = DriveApp.getFileById(spreadsheetID);
var parentFolder = spreadsheetFolder.getParents();
var folderID = parentFolder.next().getId();
var reportTemplate = DriveApp.getFileById('SLIDE ID'); // Gets the report Template
var copiedTemplate = reportTemplate.makeCopy(titleName, DriveApp.getFolderById(folderID)); //Makes a copy of the orginal and saves it in the specified folder
var newPresoID = copiedTemplate.getId();
var skeleton = SlidesApp.openById(newPresoID); //opens the new template presentation
var slides = skeleton.getSlides(); //calls the new slides
return newSlide1();
function newSlide1() {
var slide1new = slides[0]; // defining the location of the new slide 1
data.forEach(function (row) {
var templateVariable = row[0]; // First column contains variable names
var templateValue = row[1]; // Second column contains values
slide1new.replaceAllText(templateVariable, templateValue); //replaces all the text that matches the placeholders
slide1new.
});
As you can see, this code just replaces the value but does not bring in the source formatting.
Here are snapshots of the original spreadsheet data and the slides:
Source Data
Destination Slide with Placeholders
Current Result
Desired Result
Please suggest a way to fix this issue. Thank you!
Here is an example of applying the colors of a spreadsheet cell to the table cells of a slide.
My spreadsheet looks like this.
My slide looks like this.
Code.gs
function myFunction() {
try {
let spread = SpreadsheetApp.openById("xxxxxxxxxxxxxxxxxxxxxxxxxxx");
let sheet = spread.getSheetByName("Sheet1");
let range = sheet.getRange(2,1,sheet.getLastRow()-1,2);
let values = range.getDisplayValues();
let colors = range.getFontColors();
let present = SlidesApp.getActivePresentation();
let slide = present.getSlides()[0];
let table = slide.getTables()[0];
for( let i=0; i<table.getNumRows(); i++ ) {
let row = table.getRow(i);
for( let j=1; j<row.getNumCells(); j++ ) {
let text = row.getCell(j).getText();
values.some( (value,k) => {
let l = text.replaceAllText(value[0],value[1]);
if( l > 0 ) {
text.getTextStyle().setForegroundColor(colors[k][1]);
return true;
}
return false;
}
);
}
}
}
catch(err) {
console.log(err);
}
}
And the final results look like this.
Reference
TableCell
Array.some()

ScriptUI - Integer number in slider percentage

I have a script that shows a dialog window with a slider bar, next to a static text.
The text shows the percentage of the slider bar (0 to 100).
The problem is that the percentage is shown in decimals, while I would need it to show them in integers, as the next part of the code takes the number from the text and I don't need decimals.
The code is :
var Dial = new Window ("dialog");
Dial.alignChildren = ["right","center"];
var Group1 = Dial.add ("group");
var Slide = Group1.add ("slider");
Slide.minvalue = 0;
Slide.maxvalue = 100;
Slide.value = 50;
Slide.preferredSize.width = 300;
Slide.onChanging = function () {
Label.text = Slide.value;
}
var Label = Group1.add ("statictext");
Label.preferredSize.width = 30;
Label.text = Slide.value;
var Button1 = Dial.add ("button");
Button1.text = "OK";
Button1.onClick = function () {
Dial.close()
}
Dial.show()
Someone has any idea how to do it?
I've tried with Math.round() on Label.text and on Slide.value, but I don't know if I can't use it correctly or if it's not the right code for this case.
I'm sure the line like this is the absolute correct way to get the round numbers in this case:
Label.text = Math.round(Slide.value);
If somewhere down-river you will need a string you can get the string via var myString = Label.text.toString(); or even var myString = '' + Label.text;. But usually JavaScript/Extendscript doesn't care either the value is a number or a string. It deals with them indiscriminately.

Google-App-Script Conditional Loop is running but is not behaving as expected (java script)

I am pulling data from a google sheets which looks like this:
Now, I want to generate google slides for ONLY rows where the Timestamp column is between Last Deadline and Next Deadline. In the example, it would pull the record in A2:B2 as the Timestamp is between these two dates. I added this logic to my script but when I run it, it does not generate the slide, i.e. it does behave as expected but neither do I get an error. What could it be?
function generateSlides_master()
{
var dataSpreadsheetUrl = "https://docs.google.com/spreadsheets/d/1hhf";
var ss = SpreadsheetApp.openByUrl(dataSpreadsheetUrl);
var deck = SlidesApp.getActivePresentation();
var sheet = ss.getSheetByName('Form_Responses');
var values = sheet.getRange('A2:N20000').getValues();
var slides = deck.getSlides();
var templateSlide = slides[1];
var last_deadline = sheet.getRange('P4:P4').getValues();
var next_deadline = sheet.getRange('P2:P2').getValues();
values.forEach(function(page){ //for each row in google sheets
if(page[0]){
if (page[0] > last_deadline && Work_Week<= next_deadline ){ //THIS IS NOT WORKING AS EXPECTED!
var Work_Week = Utilities.formatDate(page[0], "GMT", "MM/dd/yyyy");
var Email = page[1];
templateSlide.duplicate(); //duplicate the template page
slides = deck.getSlides(); //update the slides array for indexes and length
newSlide = slides[2]; // declare the new page to update
var shapes = (newSlide.getShapes());
shapes.forEach(function(shape){
shape.getText().replaceAllText('{{Email}}',Email);
});
presLength = slides.length;
newSlide.move(presLength);
}
}// end our conditional statement
}); //close our loop of values
//Remove the template slide
//templateSlide.remove();
}
You've defined last_deadline and next_deadline to be 2-dimensional arrays, so your if-statement isn't actually checking against the dates. Use getValue() instead to get the individual values.
var last_deadline = sheet.getRange('P4').getValue();
var next_deadline = sheet.getRange('P2').getValue();
I also think you meant page[0] <= next_deadline, instead of comparing against Work_Week.
Here's a heavily edited example that will simply log the timestamp rather than creating a slide.
function generateSlides_master() {
var dataSpreadsheetUrl = "https://docs.google.com/spreadsheets/d/1hhf";
var ss = SpreadsheetApp.openByUrl(dataSpreadsheetUrl);
var sheet = ss.getSheetByName('Form_Responses');
var values = sheet.getRange('A2:N20000').getValues();
var last_deadline = sheet.getRange('P4').getValue();
var next_deadline = sheet.getRange('P2').getValue();
values.forEach(function(page) { //for each row in google sheets
var timestamp = page[0];
if (timestamp) {
if (timestamp > last_deadline && timestamp<= next_deadline) {
Logger.log('Create slide ' + timestamp);
}
}
});
}

Adding a dummy parameter to refresh a script

I'm trying to create a dummy parameter to update a cell colour checking script, however whenever I add an extra parameter, I get an error in that cell. Range not found Line 7.
I think it might be something to do with the regexp but I can't figure out how to fix it.
function countColoredCells(countRange,colorRef) {
var activeRange = SpreadsheetApp.getActiveRange();
var activeSheet = activeRange.getSheet();
var formula = activeRange.getFormula();
var rangeA1Notation = formula.match(/\((.*)\,/).pop();
var range = activeSheet.getRange(rangeA1Notation);
var bg = range.getBackgrounds();
var values = range.getValues();
var colorCellA1Notation = formula.match(/\,(.*)\)/).pop();
var colorCell = activeSheet.getRange(colorCellA1Notation);
var color = colorCell.getBackground();
var count = 0;
for(var i=0;i<bg.length;i++)
for(var j=0;j<bg[0].length;j++)
if( bg[i][j] == color )
count=count+1;
return count;
};
I got this code from: http://igoogledrive.blogspot.com/2015/11/google-spreadsheet-count-of-colored.html

How can I get the first character after the selected text in InDesign?

I want to get the two characters surrounding my selected text in indesign.
I can get the character right before the selection with:
var myStory = app.selection[0].parentStory;
var myIndex = app.selection[0].index;
var myChar1 = myStory.characters[myIndex-1];
How can I get the first character right after the selection?
I got it to work with this:
var myStory = app.selection[0].parentStory;
var myIndex = app.selection[0].index;
var toplam = app.selection[0].characters.length;
var myChar1 = myStory.characters[myIndex-1];
var myChar2 = myStory.characters[myIndex+toplam];

Categories