Home > database >  Insert a set of values in a range in apps script
Insert a set of values in a range in apps script

Time:11-29

I have a range selected and I want to insert some values in it

This is the code I have tried to put up

// I got this a5_range by offsetting from another reference cell
var a5_range = a1_range.offset(0,13);
a5_range = a5_range.offset(0,1,15,8);
a5_range.setBackground("#e6e6e6").setBorder(true, true, true, true, true, true);
const p_perc = ["40%", "20%", "30%", "21%", "35%", "28%", "25%", "13%"];
// code to include above value in a5_range
a5_range.setNumberFormat("[$$]#,##0.00");

below is the output I want for a5_range

enter image description here

Please help!

CodePudding user response:

Try it this way:

function elfunk() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  const a1_range = sh.getRange("A1");//initialize to something
  var a5_range = a1_range.offset(0, 13);
  a5_range = a5_range.offset(0, 1, 15, 8);
  a5_range.setBackground("#e6e6e6").setBorder(true, true, true, true, true, true);
  const p_perc = ["40%", "20%", "30%", "21%", "35%", "28%", "25%", "13%"];
  a5_range.setNumberFormat("[$$]#,##0.00");
  a5_range.setValues([...Array.from(new Array(a5_range.getHeight()).keys(),x => p_perc)]);
}

Demo:

A B C D E F G H I J K L M N O P Q R S T U V
1 40% 20% 30% 21% 35% 28% 25% 13%
2 40% 20% 30% 21% 35% 28% 25% 13%
3 40% 20% 30% 21% 35% 28% 25% 13%
4 40% 20% 30% 21% 35% 28% 25% 13%
5 40% 20% 30% 21% 35% 28% 25% 13%
6 40% 20% 30% 21% 35% 28% 25% 13%
7 40% 20% 30% 21% 35% 28% 25% 13%
8 40% 20% 30% 21% 35% 28% 25% 13%
9 40% 20% 30% 21% 35% 28% 25% 13%
10 40% 20% 30% 21% 35% 28% 25% 13%
11 40% 20% 30% 21% 35% 28% 25% 13%
12 40% 20% 30% 21% 35% 28% 25% 13%
13 40% 20% 30% 21% 35% 28% 25% 13%
14 40% 20% 30% 21% 35% 28% 25% 13%
15 40% 20% 30% 21% 35% 28% 25% 13%

Image:

enter image description here

  • Related