GAS如何获得最后一栏?

时间:2017-10-12 08:50:48

标签: google-apps-script google-sheets

我想知道如何从电子表格中的特定行获取最后一列。

function myFunction() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];
 var range = sheet.getRange("A:A").getValues();
 var lastrow = range.filter(String).length;

这是获取最后一行的方式,我想知道最后一列的这个版本 看这个。我想知道每一行的最后一栏。例如,A2的最后一列是“6”。 enter image description here

如果有的话,请告诉我。

2 个答案:

答案 0 :(得分:0)

这个示例脚本怎么样?在此示例中,通过使用WholePanel计算最大列中的单元格数来检索每行的最后一列。检索到的数据以JSON格式输出。

示例脚本:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;

public class WholePanel extends JPanel {

    private Color currentColor = Color.black;
    private CanvasPanel canvas;
    private JPanel topPanel;
    private JButton undo;
    private ArrayList shapeList;
    int flag = 1; //Debug
    private String currentShape = "circle";
    private int currentSize = 10;

    private ArrayList pointList;

    Shape nShape;

    JComboBox<String> comboShapes = new JComboBox<String>();
    JComboBox<Integer> comboSize = new JComboBox<Integer>();
    JComboBox<String> comboColors = new JComboBox<String>();

    public WholePanel() {
        currentColor = Color.black;
        shapeList = new ArrayList();
        pointList = new ArrayList();

        topPanel = new JPanel();

        canvas = new CanvasPanel();
        canvas.addMouseListener(new PointListener());

        JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topPanel, canvas);

        setLayout(new BorderLayout());
        add(sp);

        undo = new JButton("Undo");

        comboShapes = new JComboBox<String>();
        comboShapes.addItem("circle");
        comboShapes.addItem("square");
        comboShapes.addActionListener(new ComboListener());

        comboSize = new JComboBox<Integer>();
        comboSize.addItem(10);
        comboSize.addItem(20);
        comboSize.addItem(30);
        comboSize.addItem(40);
        comboSize.addItem(50);
        comboSize.addActionListener(new ComboListener());

        comboColors = new JComboBox<String>();
        comboColors.addItem("Black");
        comboColors.addItem("Red");
        comboColors.addItem("Blue");
        comboColors.addItem("Green");
        comboColors.addItem("Orange");
        comboColors.addActionListener(new ComboListener());

        topPanel.add(comboShapes, BorderLayout.WEST);
        topPanel.add(comboSize, BorderLayout.CENTER);
        topPanel.add(comboColors, BorderLayout.EAST);
        topPanel.add(undo, BorderLayout.EAST);
    }

    private class CanvasPanel extends JPanel {

        //this method draws all shapes 
        public void paintComponent(Graphics page) {
            super.paintComponent(page);
            setBackground(Color.WHITE);

//            page.setColor(nShape.color);
//            page.drawOval(nShape.x, nShape.y, nShape.width, nShape.height);
//            page.fillOval(nShape.x, nShape.y, nShape.width, nShape.height);
            for (int i = 0; i < shapeList.size(); i++) {
                //Point drawPoint = (Point) pointList.get(i);
                ((Shape) shapeList.get(i)).draw(page);
            }
        }

        @Override
        public void paint(Graphics g) {

            super.paint(g);
            setBackground(Color.WHITE);

            //page.setColor(nShape.color);
            //page.drawOval(nShape.x, nShape.y, nShape.width, nShape.height);
            //page.fillOval(nShape.x, nShape.y, nShape.width, nShape.height);
            for (int i = 0; i < shapeList.size(); i++) {
                //Point drawPoint = (Point) pointList.get(i);
                ((Shape) shapeList.get(i)).draw(g);
            }

        }

    }

    private class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            undo = new JButton("Undo");
            undo.addActionListener(new ButtonListener());
        }
    } // end of ButtonListener

    // listener class to set the color chosen by a user using
    // color combo box, set the size chosen using size combo box
    // or set the shape (circle or square) using shape combo box
    private class ComboListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            System.out.println("action action");
            if (event.getSource() == comboShapes) {
                currentShape = (String) comboShapes.getSelectedItem();
            }

            if (event.getSource() == comboSize) {
                currentSize = (int) comboSize.getSelectedItem();
            }

            if (event.getSource() == comboColors) {
                currentColor = Color.getColor((String) comboColors.getSelectedItem());
            }
        }
    } // end of ComboListener

    // listener class that listens to the mouse
    public class PointListener implements MouseListener {

        public void mousePressed(MouseEvent event) {
            Point pt = event.getPoint();
            //pointList.add(event.getPoint());
            if (currentShape.equals("circle")) {
                nShape = new Circle((int) pt.getX(), (int) pt.getY(), currentSize, currentSize, currentColor);
                shapeList.add(nShape);
            } else {
                nShape = new Square((int) pt.getX(), (int) pt.getY(), currentSize, currentSize, currentColor);
                shapeList.add(nShape);
            }
            repaint();
        }

        public void mouseReleased(MouseEvent event) {
        }

        public void mouseClicked(MouseEvent event) {
        }

        public void mouseEntered(MouseEvent event) {
        }

        public void mouseExited(MouseEvent event) {
        }

    } // end of PointListener

} // end of Whole Panel Class

样本表:

enter image description here

结果:

[[5,10],[10,15]]

答案 1 :(得分:0)

function getLastColumninaRow() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
  // select the first non blank cell and activate  that we want 
  // to get the Last Column that is non Blank 
  spreadsheet.getRange('H7').activate();
  // the following line will select the whole line until blank the last cell;
  spreadsheet.getSelection()
    .getNextDataRange(SpreadsheetApp.Direction.NEXT).activate();
  var Rng = spreadsheet.getActiveRange().getA1Notation()
  Logger.log('Rng %s', Rng)
  // Youll get the last Range in a Row in A1 Notation
};