Frage

Ich habe eine Tabelle mit einigen Werten in Spalte G sind einige Zellen leer dazwischen, und ich brauche den letzten Wert aus der Spalte in einer anderen Zelle zu erhalten.

So etwas wie:

=LAST(G2:G9999)

, außer dass LAST ist keine Funktion.

War es hilfreich?

Lösung

So nimmt diese Lösung einen String als Parameter. Sie findet, wie viele Zeilen in der Folie sind. Es wird alle Werte in der Spalte angegeben. Es Schleifen durch die Werte vom Ende bis zum Anfang, bis er einen Wert findet, der keine leere Zeichenfolge ist. Schließlich retunrs sie den Wert.

Script:

function lastValue(column) {
  var lastRow = SpreadsheetApp.getActiveSheet().getMaxRows();
  var values = SpreadsheetApp.getActiveSheet().getRange(column + "1:" + column + lastRow).getValues();

  for (; values[lastRow - 1] == "" && lastRow > 0; lastRow--) {}
  return values[lastRow - 1];
}

Verwendung:

=lastValue("G")

EDIT:

Als Reaktion auf den Kommentar für die Funktion Update fragt automatisch:

Der beste Weg, den ich finden konnte, dies verwenden, um mit dem Code oben:

function onEdit(event) {
  SpreadsheetApp.getActiveSheet().getRange("A1").setValue(lastValue("G"));
}

Es wäre nicht mehr erforderlich sein, um die Funktion in einer Zelle zu verwenden, wie die Verwendung Staaten. Stattdessen sind Sie auf die Zelle harte Kodierung würden Sie aktualisieren möchten und die Spalte würden Sie verfolgen möchten. Es ist möglich, dass es eine beredte Weise ist diese (hoffentlich eine, das nicht hart codiert) zu implementieren, aber das ist das Beste, was ich für jetzt finden konnte.

Beachten Sie, wenn Sie die Funktion in der Zelle verwenden, wie bereits erwähnt, ist es nach dem Neuladen aktualisiert. Vielleicht gibt es eine Möglichkeit, in onEdit() einzuhaken und in Zellfunktionen zu aktualisieren erzwingen. Ich kann es einfach nicht in der Dokumentation finden.

Andere Tipps

ähnliche Antwort auf caligari Antwort , aber wir können es aufräumen, indem nur den vollständigen Spaltenbereich spezifiziert:

=INDEX(G2:G, COUNT(G2:G))

Eigentlich fand ich eine einfachere Lösung hier:

http://www.google. com / support / forum / p / Google + Docs / Thread? tid = 20f1741a2e663bca & hl = en

Es sieht wie folgt aus:

=FILTER( A10:A100 , ROW(A10:A100) =MAX( FILTER( ArrayFormula(ROW(A10:A100)) , NOT(ISBLANK(A10:A100)))))

LAST () Funktion wird zur Zeit nicht implementiert, um die letzte Zelle in einem Bereich zu wählen. Doch nach Ihrem Beispiel:

=LAST(G2:G9999)

Wir sind in der Lage letzte Zelle mit dem Paar von Funktionen INDEX () und COUNT () auf diese Weise zu erhalten:

=INDEX(G2:G; COUNT(G2:G))

Es gibt ein anschauliches Beispiel am spreedsheet, wo ich (und gelöst) das gleiche Problem (Blatt Orzamentos, Zelle I5) gefunden zu haben. Beachten Sie, dass es perfekt funktioniert sogar beziehen auf andere Blätter innerhalb des Dokuments.

Zusammenfassung:

=INDEX( FILTER( G2:G , NOT(ISBLANK(G2:G))) , COUNTA(G2:G) )

Details:

Ich habe mehrere Antworten sah durch und versucht, und hier ist das, was ich gefunden habe: Die einfachste Lösung (siehe Dohmoose‘Antwort ) funktioniert, wenn es keine Leerzeichen:

=INDEX(G2:G; COUNT(G2:G))

Wenn Sie Rohlingen haben, es funktioniert nicht.

Sie können eine leere Griff nur um von COUNT zu COUNTA wechseln (Siehe user3280071 Antwort ):

=INDEX(G2:G; COUNTA(G2:G))

Allerdings wird dies für einige Kombinationen von Rohlingen scheitern. (1 blank 1 blank 1 nicht für mich.)

Der folgende Code funktioniert (Siehe Nader Antwort und Jasons Kommentar ):

=INDEX( FILTER( G2:G , NOT(ISBLANK(G2:G))) , ROWS( FILTER( G2:G , NOT(ISBLANK(G2:G)) ) ) )

, aber es erfordert darüber nachzudenken, ob Sie wollen oder COLUMNS ROWS für einen bestimmten Bereich verwenden.

Wenn jedoch COLUMNS mit COUNT ersetzt Ich scheine eine zuverlässige, blank sichere Implementierung von LAST zu bekommen:

=INDEX( FILTER( G2:G , NOT(ISBLANK(G2:G))) , COUNT( FILTER( G2:G , NOT(ISBLANK(G2:G)) ) ) ) 

Und da COUNTA die Filter eingebaut haben, können wir vereinfachen weiter mit

=INDEX( FILTER( G2:G , NOT(ISBLANK(G2:G))) , COUNTA(G2:G) )

Das ist etwas einfach und korrekt. Und Sie müssen sich keine Sorgen machen, ob Zeilen oder Spalten zu zählen. Und im Gegensatz zu Skript-Lösungen, wird es automatisch aktualisiert mit Änderungen an die Tabelle.

Und wenn Sie den letzten Wert in einer Reihe bekommen mögen, nur den Datenbereich ändern:

=INDEX( FILTER( A2:2 , NOT(ISBLANK(A2:2))) , COUNTA(A2:2) )

Um den letzten Wert aus einer Spalte von Textwerten zurückzukehren, die Sie benötigen COUNTA zu verwenden, so dass Sie diese Formel brauchen würden:

=INDEX(G2:G; COUNTA(G2:G))

versuchen Sie dies: =INDIRECT("B"&arrayformula(max((B3:B<>"")*row(B3:B))))

Angenommen, die Spalte, in der Sie zum letzten Wert sucht, ist B.

Und ja, es funktioniert mit Leerzeichen.

Es sieht aus wie Google Apps Script jetzt Bereiche als Funktionsparameter unterstützt. Diese Lösung nimmt einen Bereich:

// Returns row number with the last non-blank value in a column, or the first row
//   number if all are blank.
// Example: =rowWithLastValue(a2:a, 2)
// Arguments
//   range: Spreadsheet range.
//   firstRow: Row number of first row. It would be nice to pull this out of
//     the range parameter, but the information is not available.
function rowWithLastValue(range, firstRow) {
  // range is passed as an array of values from the indicated spreadsheet cells.
  for (var i = range.length - 1;  i >= 0;  -- i) {
    if (range[i] != "")  return i + firstRow;
  }
  return firstRow;
}

Siehe auch Diskussion in Google Apps Script-Hilfe-Forum:

I looked at the previous answers and they seem like they're working too hard. Maybe scripting support has simply improved. I think the function is expressed like this:

function lastValue(myRange) {
    lastRow = myRange.length;
    for (; myRange[lastRow - 1] == "" && lastRow > 0; lastRow--)
    { /*nothing to do*/ }
    return myRange[lastRow - 1];
}

In my spreadsheet I then use:

= lastValue(E17:E999)

In the function, I get an array of values with one per referenced cell and this just iterates from the end of the array backwards until it finds a non-empty value or runs out of elements. Sheet references should be interpreted before the data is passed to the function. Not fancy enough to handle multi-dimensions, either. The question did ask for the last cell in a single column, so it seems to fit. It will probably die on if you run out of data, too.

Your mileage may vary, but this works for me.

function lastRow(column){
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var lastRow = sheet.getLastRow();
  var lastRowRange=sheet.getRange(column+startRow);
  return lastRowRange.getValue();
}

no hard coding.

This one works for me:

=INDEX(I:I;MAX((I:I<>"")*(ROW(I:I))))

This gets the last value and handles empty values:

=INDEX(  FILTER( H:H ; NOT(ISBLANK(H:H))) ; ROWS( FILTER( H:H ; NOT(ISBLANK(H:H)) ) ) )

In a column with blanks, you can get the last value with

=+sort(G:G,row(G:G)*(G:G<>""),)

The answer

$ =INDEX(G2:G; COUNT(G2:G))

doesn't work correctly in LibreOffice. However, with a small change, it works perfectly.

$ =INDEX(G2:G100000; COUNT(G2:G100000))

It always works only if the true range is smaller than (G2:G10000)

Is it acceptable to answer the original question with a strictly off topic answer:) You can write a formula in the spreadsheet to do this. Ugly perhaps? but effective in the normal operating of a spreadsheet.

=indirect("R"&ArrayFormula(max((G:G<>"")*row(G:G)))&"C"&7)


(G:G<>"") gives an array of true false values representing non-empty/empty cells
(G:G<>"")*row(G:G) gives an array of row numbers with zeros where cell is empty
max((G:G<>"")*row(G:G)) is the last non-empty cell in G

This is offered as a thought for a range of questions in the script area that could be delivered reliably with array formulas which have the advantage of often working in similar fashion in excel and openoffice.

function getDashboardSheet(spreadsheet) {
  var sheetName = 'Name';
  return spreadsheet.getSheetByName(sheetName);
}
      var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);  
      var dashboardSheet = getDashboardSheet(spreadsheet);
      Logger.log('see:'+dashboardSheet.getLastRow());

I was playing with the code given by @tinfini, and thought people might benefit from what I think is a slightly more elegant solution (note I don't think scripts worked quite the same way when he created the original answer)...

//Note that this function assumes a single column of values, it will 
//not  function properly if given a multi-dimensional array (if the 
//cells that are captured are not in a single row).

function LastInRange(values) 
{
  for (index = values.length - 1; values[index] == "" && index > 0; index--) {}
  return String(values[index]);
}

In usage it would look like this:

=LastInRange(D2:D)

Regarding @Jon_Schneider's comment, if the column has blank cells just use COUNTA()

=INDEX(G2:G; COUNT**A**(G2:G))

Found a slight variation that worked to eliminate blanks from the bottom of the table. =index(G2:G,COUNTIF(G2:G,"<>"))

I'm surprised no one had ever given this answer before. But this should be the shortest and it even works in excel :

=ARRAYFORMULA(LOOKUP(2,1/(G2:G<>""),G2:G))

G2:G<>"" creates a array of 1/true(1) and 1/false(0). Since LOOKUP does a top down approach to find 2 and Since it'll never find 2,it comes up to the last non blank row and gives the position of that.

The other way to do this, as others might've mentioned, is:

=INDEX(G2:G,MAX((ISBLANK(G2:G)-1)*-ROW(G2:G))-1)

Finding the MAXimum ROW of the non blank row and feeding it to INDEX

In a zero blank interruption array, Using INDIRECT RC notation with COUNTBLANK is another option. If V4:V6 is occupied with entries, then,

V18:

=INDIRECT("R[-"&COUNTBLANK(V4:V17)+1&"]C",0)

will give the position of V6.

to get the last value from a column you can also use MAX function with IF function

=ARRAYFORMULA(INDIRECT("G"&MAX(IF(G:G<>"", ROW(G:G), )), 4)))
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top