【问题标题】:Insert a set of values in a range in apps script在应用程序脚本的范围内插入一组值
【发布时间】:2022-11-29 00:38:32
【问题描述】:

我选择了一个范围,我想在其中插入一些值

这是我试图提出的代码

// 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");

下面是我想要的 a5_range 的输出

请帮忙!

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    试试这样:

    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)]);
    }
    

    演示:

    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%

    图片:

    【讨论】:

    • 这就像一个魅力,请你帮我理解这个[...Array.from(new Array(a5_range.getHeight()).keys(),x => p_perc)])。您正在创建一个数组并使用“getHeight”获取范围内的行数,.keys() 和 x => p_perc 是什么
    • 你用过 Array.from 吗?或新数组()?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 2022-12-03
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 2018-08-30
    相关资源
    最近更新 更多