Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your specific question was:</p> <blockquote> <p>My question: Is it possible for the script to know the A1notation of the 1st occurrence of a value and add a row above it?</p> </blockquote> <p>By using a combination of a Boolean to indicate whether you've found <em>any</em> of the items you are looking for, and a couple of <code>Range</code> methods, you can get the A1notation of the first occurrence. </p> <pre><code>var column = 1; // this example is only using column A var foundGerman = false; var firstGerman = ''; ... if (lang[i] == 'German') { if (!foundGerman) { foundGerman = true; firstGerman = range.getCell(i, column).getA1Notation(); } ... } </code></pre> <p>After that, you will have the A1Notation of the first cell containing "German".</p> <p>However, the function <code>insertRowsBefore()</code> is a <code>Sheet</code> method, and expects a row number as a parameter, not A1Notation. So figuring out what the address of the first German cell was turns out to be unnecessary. </p> <h2>Script</h2> <p>Here's my entry in the contest! For speed, it's wise to use as few apps script service calls as possible. In this script, all data manipulation is done using arrays, with the final result written once. </p> <p>In anticipation that you'll have more than three languages that you care about, and for maintainability, the language lookup is handled by an Object, <code>iso639</code>. (Assuming you're using <a href="http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes" rel="nofollow">ISO 639-1 Language Codes</a>.) As a result, the actual work takes just 10 lines of code!</p> <pre><code>function insertViaArray() { var sheet = SpreadsheetApp.getActive().getSheetByName('REPORT'); var values = sheet.getDataRange().getValues(); var newValues = []; var iso639 = { "German" : "DE", "Italian" : "IT", "French" : "FR" } var curLang = ''; for (var i in values) { if (values[i][0] !== curLang) { curLang = values[i][0]; newValues.push([iso639[curLang]]); } newValues.push([values[i][0]]); } sheet.getRange(1, 1, newValues.length, 1).setValues(newValues) }; </code></pre> <p><strong>Edit</strong> Script V2</p> <p>By using <code>Array.splice()</code> to insert the language tags, we can further reduce the code to 8 working lines, and eliminate the need for a parallel <code>newValues</code> array.</p> <pre><code>function insertViaArrayV2() { var sheet = SpreadsheetApp.getActive().getSheetByName('REPORT'); var values = sheet.getDataRange().getValues(); var iso639 = { "German" : "DE", "Italian" : "IT", "French" : "FR" } var curLang = ''; for (var i = 0; i &lt; values.length; i++) { if (values[i][0] !== curLang) { curLang = values[i][0]; values.splice(i, 0, [iso639[curLang]]); } } sheet.getRange(1, 1, values.length, 1).setValues(values) }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload