Google script change sheet cell to fit image - youtube-api

Below sample script will get youtube thumbnail and show it in sheet cells. How can we make sure the cell width/height fit the image width/height? Current script will shrink the image to cell so it's too small.
I try to call autoResizeColumn API but it seems doesn't work for image.
function searchYoutubeByKeyword() {
var results = YouTube.Search.list('id,snippet', {
q:'Cpp',
order:'date',
maxResults: 20
});
var lr = 1;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("Sheet1");
for (var i = 0; i < results.items.length; i++) {
var item = results.items[i];
Logger.log('[%s] Title: %s', item.id.videoId,item.snippet.publishedAt);
var url = 'http://www.youtube.com/watch?v='+item.id.videoId;
var desc = item.id.videoId;
var link = '=HYPERLINK'+'("' + url+'","'+ desc +'")';
sheet1.getRange(lr, 1).setValue(link);
sheet1.getRange(lr, 2).setValue(item.snippet.publishedAt.substring(0,10));
sheet1.getRange(lr, 3).setValue(item.snippet.title);
var thumbnail = item.snippet.thumbnails["medium"].url;
sheet1.getRange(lr, 4).setValue('=image("'+thumbnail+'")');
lr++;
}
sheet1.getRange(1,1,sheet1.getLastRow(),3).setNumberFormat('#STRING#');
sheet1.autoResizeColumn(4);
}

Related

CopyTo Google sheets

I'm trying to get this code to work but keep getting a "TypeError: offset.copyTo is not a function". The script is supposed to take one value from sheet "Budget" and copy it into the next available row in column F on sheet "Projected".
I have tried playing around with syntax and other ways to copy but to now avail, any help would be greatly appreciated
function OffsetRecord() {
var sheet = SpreadsheetApp.openById("xx").getSheetByName("Budget");
var offset = sheet.getRange('I5').getValue();
var destSheet = SpreadsheetApp.openById("xx").getSheetByName("Projected");
var colF = destSheet.getRange('F:F').getValues();
var fcolF = colF.filter(function (e) {return e[0];});
var firstEmptyRowIncolF = fcolF.indexOf('')+1;
offset.copyTo(firstEmptyRowIncolF, {contentsOnly: true});
copyTo expects ranges as parameters, not values or column numbers, see here
Modify your code as following:
function OffsetRecord() {
var spreadsheet = SpreadsheetApp.openById("XX");
var sheet = spreadsheet.getSheetByName("Budget");
var offset = sheet.getRange('I5');//without ".getValue();"!
var destSheet = spreadsheet.getSheetByName("Projected");
...
firstEmptyRowIncolF = ...;
var destinationColumn = 1;
var range = destSheet.getRange(firstEmptyRowIncolF, destinationColumn);
offset.copyTo(range, {contentsOnly: true});
}

How to count a cell which is green in color in google sheet

I have a case where i need to count if a cell has color of "green" in google sheet.
I tried few adons but they didn't work. I am looking for a solution via a script or a formula.
I have around 4 green values as shown in the attachment below
Tried this but it returns zero
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
//define here range of interest
var range = sheet.getRange("BG4:BO");
var backgrounds = range.getBackgrounds();
var counter = 0;
var green = "00ff00"
for (var i = 0; i < backgrounds.length; i++){
if(backgrounds[i]== "#00ff00")
counter++;
}
Logger.log(counter);
sheet.getRange(3, 59).setValue(counter);
}
Thanks
You can count the green cells with Google Apps Script using the method getBackgrounds()
Sample:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
//define here range of interest
var range = sheet.getRange("A1:A10");
var backgrounds = range.getBackgrounds();
var counter = 0;
var green = "#00ff00";
for (var i = 0; i < backgrounds.length; i++){
if(backgrounds[i]== green)
counter++;
}
Logger.log(counter);
}
UPDATE
If you want to run the function on more than one column, you need to convert the 2D backgrounds array into a 1D one in order to run the function above. This can be accomplished with flat(). So:
...
var backgrounds = range.getBackgrounds();
backgrounds = backgrounds.flat();
var counter = 0;
...

Google Sheets: Change background color of cell using content of another cell

I have a google sheet as above where I am typing in colour codes in a hex format and then naming them. I would like to automatically update the Colour Block column's background colour with the hex code in the code column.
Script I have tried, but setBackground function doesn't work.
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var cells = sheet.getDataRange().getValues();
for(n = 1; n < cells.length; n++) {
var cell = cells[n];
cell[n][2].setBackground(cell[n][1]);
}
}
I did manage to make this work with the following:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
for(r = 1; r < values.length; r++) {
var row = values[r];
var code = row[1];
range.getCell(r+1,3).setBackground(code);
}
}

Search within a spreadsheet a cell, and change another

I'm trying to create a script in google sheets. What I'm trying to do is to search the value of a cell (let's say A2 sheet1), then change the value of another cell (B2 sheet2). When the first iteration is over, proceed to the next row (A3 sheet1) and again change the value of B2. Also wait 10 secs at the end of every iteration.
I don't know why it is not working. After I run the code it just says "Finished", but nothing has changed.
function test() {
var ss = SpreadsheetApp.getActive();
var gensheet = ss.getSheetByName('Generador SE');
var clientsheet = ss.getSheetByName('Clientes SE');
var clientrange = clientsheet.getRange("A1:C200");
var clientsquantity = clientrange.getNumRows()
var servicetochange = gensheet.getRange(2,2)
var i;
for (i=1;i>clientsquantity;i++){
var currentservice = clientrange.getRange(i,1).getValue;
servicetochange=gensheet.getRange().setValue(currentservice);
Utilities.sleep(10000);
}
}
I have changed some code in the loop ... now it does something (but I can't be sure it does what you expect):
function test() {
var ss = SpreadsheetApp.getActive();
var gensheet = ss.getSheetByName('Generador SE');
var clientsheet = ss.getSheetByName('Clientes SE');
var clientrange = clientsheet.getRange("A1:C200");
var clientsquantity = clientrange.getNumRows()
var servicetochange = gensheet.getRange(2,2)
var i;
for (i=1;i<clientsquantity;i++){
var currentservice = clientrange.offset(i,0,1,1).getValue();
servicetochange.offset(i,0,1,1).setValue(currentservice);
Utilities.sleep(10000);
}
}

How to count cell values based on the background color in Spreadsheet

I am trying to count the values in a cell based on the background color. Someone I got some help while online search I am not getting the complete solution. Does anyone help me out with this prob. I have sample code script which I got from the online search.
Also the script which am pasting here is grouping the values( For Example if i have a value A in three cells it should return the value as 3 instead it is returning AAA. Can someone help me out with the script to count the values and return it based on the background color
Thanks in Advance,
Here is the script:
function sumBackgroundColors(rangeString, color) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var sumRange = s.getRange(rangeString);
var sum = 0;
var sumRangeBackground = sumRange.getBackgroundColors();
var sumRangeValues = sumRange.getValues();
for(var row = 0; row < sumRangeBackground.length; row++ ) {
for(var col = 0; col < sumRangeBackground[0].length; col++ ) {
if( sumRangeValues[row][col]=="LG M"&& sumRangeBackground[row][col] == color ) {
sum = sum + parseFloat(sumRangeValues[row][col]);
}
}
}
return sum;
}
I recommend you to not use a custom function to do this because of its caching feature (explanation here).
function countBackgroundColors() {
var rangeString = "A1:A100";
var color = "green";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var range = s.getRange(rangeString);
var count = 0;
var rangeColors = range.getBackgroundColors();
for( var i in rangeColors )
for( var j in rangeColors[i] )
if( rangeColors[i][j] == color )
++count;
return count;
}

Resources