{"id":6022,"date":"2013-10-23T21:52:43","date_gmt":"2013-10-23T18:52:43","guid":{"rendered":"http:\/\/railsware.com\/blog\/?p=6022"},"modified":"2021-08-12T16:52:20","modified_gmt":"2021-08-12T13:52:20","slug":"google-spreadsheet-scripts-useful-functions-copying-with-custom-conditions","status":"publish","type":"post","link":"https:\/\/railsware.com\/blog\/google-spreadsheet-scripts-useful-functions-copying-with-custom-conditions\/","title":{"rendered":"Google spreadsheet scripts: useful functions. Copying with custom conditions"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Problem<\/h2>\n\n\n\n<p>You need to copy a bunch of data from one spreadsheet to another with the following custom conditions: limited range and filters on different cell values. This is a very boring task to perform manually, but the good news is that it can be automated.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Solution<\/h2>\n\n\n\n<p>In order to solve the task, I&#8217;ve created <i>CopyDataFromSStoAnotherSSWithComplexFilter<\/i> function. It receives the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>source spreadsheet id<\/li><li>source sheet name<\/li><li>complex filter &#8211; limit data with combination of simpler filters<\/li><li>limit range of data to be processed<\/li><li>target spreadsheet id<\/li><li>target sheet name<\/li><li>target cell position &#8211; initial place for copied data<\/li><li>arrangement method &#8211; insert as new rows or replace existing cell values<\/li><\/ul>\n\n\n\n<p>Let\u2019s try this function on the example we have available.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Working example<\/h2>\n\n\n\n<p>Open <a title=\"Useful Google Scripts Example 1\" href=\"http:\/\/l.rw.rw\/example\" target=\"_blank\" rel=\"noopener noreferrer\">example spreadsheet<\/a> and wait for 5 seconds for these menus to appear:<a href=\"https:\/\/railsware.com\/blog\/wp-content\/uploads\/2013\/10\/2013-10-21_1311.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6026 size-full\" src=\"https:\/\/railsware.com\/blog\/wp-content\/uploads\/2013\/10\/2013-10-21_1311.png\" alt=\"2013-10-21_1311\" width=\"735\" height=\"270\"><\/a><br>Our example has 3 sheets:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>config_data &#8211; keeps all settings for our example<\/li><li>source_data &#8211; data which will be copied<\/li><li>target_data &#8211; contains result of our example execution<\/li><\/ul>\n\n\n\n<p>To copy data use <i>&#8220;Copy data with config_data settings&#8221;<\/i> menu item from <i>&#8220;Run Functions Examples&#8221;<\/i>.<br>You can change settings in B column of <i>config_data<\/i> sheet and see how it affects <i>target_data<\/i> sheet. But in order to be able to customize them properly, let&#8217;s discuss how to cook filter conditions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Complex Filter<\/h2>\n\n\n\n<p>Complex filter combines ordinary filters with AND, OR operators. Ordinary filter is an array &#8211; <i>[column, filteringValue, filteringCondition]<\/i>. Where <i>column<\/i> &#8211; number of column (starts from 0), which is compared to <i>filteringValue<\/i> with <i>filteringCondition<\/i> (\u2018==\u2019, \u2018!=\u2019, \u2018&gt;\u2019, \u2018&lt;\u2019, \u2018&gt;=\u2019, \u2018=&lt;\u2019, \u2018contains\u2019). For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var ordinaryFilter = [0,\u201dsolar\u201d, \u201ccontains\u201d];<\/pre>\n\n\n\n<p>This condition leaves only rows containing &#8220;solar&#8221; word in A column (0 index).<br>Array of ordinary filters is combined with AND operators:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var condition1withAND = [ordinaryFilter1, ordinaryFilter2, ordinaryFilter3];<\/pre>\n\n\n\n<p>Array of different conditionswithAND is combined with OR operator:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var complexfilter = [condition1withAND, condition2withAND, condition3withAND]<\/pre>\n\n\n\n<p>It means that returned data will meet <i>condition1withAND<\/i> OR <i>condition2withAND<\/i> OR <i>condition3withAND<\/i>.<br><a href=\"https:\/\/railsware.com\/blog\/wp-content\/uploads\/2013\/10\/2013-10-21_1744.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6025 size-full\" src=\"https:\/\/railsware.com\/blog\/wp-content\/uploads\/2013\/10\/2013-10-21_1744.png\" alt=\"2013-10-21_1744\" width=\"638\" height=\"298\"><\/a><\/p>\n\n\n\n<p>Thus, we can build any complex condition. And there is another powerful option &#8211; limit source data by range.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Source Data Range Limit<\/h2>\n\n\n\n<p>Next array limits source data by range:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var sourceDataRangeLimit = [startRow, startColumn, numRows, numColumns];<\/pre>\n\n\n\n<p>Currently we support only one range limit.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Two ways to copy prepared data to the target sheet<\/h2>\n\n\n\n<p>Filtered and limited data can be inserted (default):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var isReplace = false;<\/pre>\n\n\n\n<p>or replaced:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var isReplace = true;<\/pre>\n\n\n\n<p>For inserting, bunch of empty rows is created starting from specified target position and data is copied there.<br>For replacing, keep in mind that existing data will be overwritten.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>We&#8217;ve described all the key points of our example and <a title=\"Useful Google Scripts Example 1\" href=\"http:\/\/l.rw.rw\/example\" target=\"_blank\" rel=\"noopener noreferrer\">you can start playing with it<\/a>. In the next post I&#8217;m going to describe <i>DeleteByRangeWithComplexFilter<\/i> function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Posts<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a title=\"Intro to Google Scripts\" href=\"http:\/\/l.rw.rw\/gscripts\" target=\"_blank\" rel=\"noopener noreferrer\">Intro to Google Application Scripts (GAS)<\/a><\/li><li><a title=\"Useful Triggers in Google Scripts\" href=\"http:\/\/l.rw.rw\/gscripts2\" target=\"_blank\" rel=\"noopener noreferrer\">Useful Triggers in Google Application Scripts<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem You need to copy a bunch of data from one spreadsheet to another with the following custom conditions: limited range and filters on different cell values. This is a very boring task to perform manually, but the good news is that it can be automated. Solution In order to solve the task, I&#8217;ve created&#8230;<\/p>\n","protected":false},"author":54,"featured_media":9455,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[],"coauthors":["Oleg Shaydenko"],"class_list":["post-6022","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"aioseo_notices":[],"categories_data":[{"name":"Engineering","link":"https:\/\/railsware.com\/blog?category=development"}],"post_thumbnails":"https:\/\/railsware.com\/blog\/wp-content\/uploads\/2013\/10\/2013-10-21_1311.png","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/6022","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/users\/54"}],"replies":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/comments?post=6022"}],"version-history":[{"count":114,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/6022\/revisions"}],"predecessor-version":[{"id":13983,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/6022\/revisions\/13983"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media\/9455"}],"wp:attachment":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media?parent=6022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/categories?post=6022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/tags?post=6022"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/coauthors?post=6022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}