Array.push.setAnyFormatting('红')? [英] Array.push.setAnyFormatting('red')?

查看:338
本文介绍了Array.push.setAnyFormatting('红')?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说明:

堆栈溢出用户mhawksey最近做了一些<一个href=\"http://stackoverflow.com/questions/35289183/long-processing-time-likely-due-to-getvalue-and-cell-inserts\">fantastic我的code优化,并在这样做,给我介绍了超高效推阵。但是,使用数组是怎么样的困难,因为我似乎无法能够使用我可以使用传统的.getRange / .setValue方法时功能。

问题:

我需要整合.setFontColors('红')和.setBackgroundColors(白)。

code和图片:

首先,我将张贴code。二,的目前的code做什么的图像。第三,code什么的图像的需求的做。

 函数格式(){  VAR SS = S preadsheetApp.getActiveS preadsheet();
  变种S = ss.getActiveSheet();
  变种LASTROW = s.getLastRow();
  VAR排;  //列得到了所有值的[] []
  VAR数据= s.getRange(A:A)的GetValues​​();
  //我们要建立一个[] []输出结果
  VAR输出= [];  //通过在A列中的所有单元格环
  为(行= 0;&行LT; LASTROW;排++){
    VAR cellValue =数据[行] [0];
    VAR破折号= FALSE;
    如果(typeof运算cellValue ==='串'){
      破折号= cellValue.substring(0,1);
    //如果一个号码复制到我们的输出数组
    }其他{
      output.push([cellValue]);
    }    //如果-dash
    如果(破折号=== - ){
      //先建+姓
      变种名称=(数据[(行+ 1)] [0] ++数据[(行+ 2)] [0])修剪();
      //为-state添加行(例如-MI)
      output.push([cellValue]);
      output.push([名]);
      output.push([顺序完成]);
      //添加一个空白行
      output.push([]);
      //跳一个额外的行加快速度
      行++;
    }
  }
  //设置我们我们的产量[] []数组中所作的值
  s.getRange(1,1,output.length).setValues​​(输出);
}

这是code做了什么:

前后

这是我想要实现的:

颜色客观

更新:

我已经附加了一个简单,工作格式化循环。问题是,当我上的数据,再列运行它,它需要很长时间才能处理。从我了解的意见,我不能快速格式化A S preadsheet。难道我错了吗?

追加格式code:

  //其它变量
VAR范围1;  //通过A列循环
  对于(VAR行= 0;&行LT; LASTROW;排++){
    范围1 = s.getRange(行+ 1,1);    //定义偏移量if语句
    变种偏移1 = range1.offset(1,0);
    变种偏移2 = range1.offset(2,0);    //子不能在数字上运行,所以...
    cellValue = range1.getValue();
    如果(typeof运算cellValue ==='号'){继续;};
    破折号= cellValue.substring(0,1);    //如果 -
    如果(破折号=== - ){
       offset1.setFontColor('红');
       offset2.setBackground('绿色');
     };
   };


解决方案

您可以使用所有的各种小号preadsheet方法为获得设置颜色,字体大小,字体粗细等的的和的的不同的阵列,但不能混用在一个单项这些属性。请参见 rel=\"nofollow\">的文档。

甚至更简便,在你的脚本编辑器编写一个脚本,定义了一个范围,并自动完成玩玩看一切你可以用它做...

(控制+空格键)

在这里输入的形象描述


编辑下面你code更新。

您应该创建握着你的范围内的所有背景颜色第二阵列,并根据您的需要填充它。
在code下面我建立parrallels阵列与你的输出数组,不是很优雅,而是系统地展示它是如何工作的。

需要注意的是空值意味着无背景色,而#0F0是十六进制code为绿色的,但你也可以使用绿色的字符串,如果你preFER ...

  ...
    VAR输出= [];
    VAR背景= []
    //通过在A列中的所有单元格环
    为(行= 0;&行LT; LASTROW;排++){
      VAR cellValue =数据[行] [0];
      VAR破折号= FALSE;
      如果(typeof运算cellValue ==='串'){
        破折号= cellValue.substring(0,1);
        //如果一个号码复制到我们的输出数组
      }其他{
        output.push([cellValue]);
        backGrounds.push([空​​]);
      }      //如果-dash
      如果(破折号=== - ){
        //先建+姓
        变种名称=(数据[(行+ 1)] [0] ++数据[(行+ 2)] [0])修剪();
        //为-state添加行(例如-MI)
        output.push([cellValue]);
        backGrounds.push([空​​]);
        output.push([名]);
        backGrounds.push([空​​]);
        output.push([顺序完成]);
        backGrounds.push(['#0F0']);
        //添加一个空白行
        output.push([]);
        backGrounds.push([空​​]);
        //跳一个额外的行加快速度
        行++;
      }
    }
  s.getRange(1,1,output.length).setBackgrounds(背景);
...

Description:

Stack Overflow user mhawksey recently did some fantastic optimization of my code, and in doing so, introduced me to super efficient array pushes. But working with arrays is kind of difficult, because I can't seem to be able to use functions I can when using the traditional .getRange/.setValue approach.

Problem:

I need to integrate .setFontColors('red') and .setBackgroundColors('white').

Code and Images:

First, I will post the code. Second, an image of what the currently code does. Third, an image of what the code needs to do.

function format() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getActiveSheet();
  var lastRow = s.getLastRow();
  var row;

  //gets a [][] of all values in the column
  var data = s.getRange("A:A").getValues();
  //we are going to build a [][] to output result
  var output = [];

  //loop through all cells in column A
  for (row = 0; row < lastRow; row++) {
    var cellValue = data[row][0];
    var dash = false;
    if (typeof cellValue === 'string') {
      dash = cellValue.substring(0, 1);
    //if a number copy to our output array
    } else {
      output.push([cellValue]);
    }

    //if -dash
    if (dash === "-") {
      //build first + last name
      var name = (data[(row+1)][0]+" "+data[(row+2)][0]).trim();
      //add row for the -state (e.g. -MI)
      output.push([cellValue]);
      output.push([name]);
      output.push(["Order complete"]);
      //add a blank row
      output.push([""]);
      //jump an extra row to speed things up
      row++;
    }
  }
  //set the values we've made in our output [][] array
  s.getRange(1, 1, output.length).setValues(output);
}

This is what the code does:

This is what I'm trying to achieve:

Update:

I've appended a simple, working formatting loop. The problem is, when I run it on a longer column of data, it takes too long to process. From what I understand of the comments, I cannot quickly format a spreadsheet. Am I wrong?

Appended formatting code:

//other variables
var range1;

  //loop through column A
  for (var row = 0; row < lastRow; row++) {
    range1 = s.getRange(row + 1, 1);

    //define offsets for if statement
    var offset1 = range1.offset(1, 0);
    var offset2 = range1.offset(2, 0);

    //substring cannot run on numbers, so...
    cellValue = range1.getValue();
    if (typeof cellValue === 'number') {continue;};
    dash = cellValue.substring(0, 1);

    //if -
    if (dash === "-") {
       offset1.setFontColor('red');
       offset2.setBackground('green');
     };
   };

解决方案

You can use all the various spreadsheet methods to get and set colors, font sizes, font weights, etc. to and from distinct arrays but you can not mix these "attributes" in one single item. See the doc here.

Or even handier, in your script editor write a script that defines a range and play with the auto complete to see everything you can do with it...

(control+space keys)


edit following your code update.

You should create a second array that holds all the background colors of your range and fill it according to your needs. In the code below I build the array in parrallels with your output array, not very elegantly but rather systematically to show how it works.

Note that null value means "no background color" while #0F0 is the hexadecimal code for green but you can also use the 'green' string if you prefer...

    ...
    var output = [];
    var backGrounds=[]
    //loop through all cells in column A
    for (row = 0; row < lastRow; row++) {
      var cellValue = data[row][0];
      var dash = false;
      if (typeof cellValue === 'string') {
        dash = cellValue.substring(0, 1);
        //if a number copy to our output array
      } else {
        output.push([cellValue]);
        backGrounds.push([null]);
      }

      //if -dash
      if (dash === "-") {
        //build first + last name
        var name = (data[(row+1)][0]+" "+data[(row+2)][0]).trim();
        //add row for the -state (e.g. -MI)
        output.push([cellValue]);
        backGrounds.push([null]);
        output.push([name]);
        backGrounds.push([null]);
        output.push(["Order complete"]);
        backGrounds.push(['#0F0']);
        //add a blank row
        output.push([""]);
        backGrounds.push([null]);
        //jump an extra row to speed things up
        row++;
      }
    }
  s.getRange(1, 1, output.length).setBackgrounds(backGrounds);
...

这篇关于Array.push.setAnyFormatting('红')?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆