How to loop google-Apps script? - api

I would like to gather subscribers for the channel id of youtube with an app script on Google Spreadsheets. But it only worked with one id.
I'd like to use a repeat statement, but I can't because I'm not good enough. I'd appreciate it if you could help me.
function getChannelInfo() {
var ss = SpreadsheetApp.getActiveSheet();
var channel = ss.getRange("B2").getValue();
var data = YouTube.Channels.list('statistics', {id: channel});
var item = data.items[0];
var info = [item.statistics.subscriberCount];
ss.getRange("C2").setValues([info]);
}
I want to gather subscribers for all video IDs in column B, but only one comes out. Help me.
https://docs.google.com/spreadsheets/d/1Ks_tE9uDdOcTKML8jf_40lJpcImMoh-JYivpV38zpCk/edit?usp=sharing

No problem
In this statement var channel = ss.getRange("B2").getValue();, you are calling just one value. Instead you should go with an array of values. Like ss.getrange(row, colum, number of rows).getdisplayvalues();
use FOR LOOP to run for each value in the above array and set values in the Spreadsheet in the corressponding cells.
Let me know if you need help :)

Related

How to use regex URL to query a Google Spreadsheet

The following queries column A in a published Google Spreadsheet. The result is all rows with text comment in them.
var word = "comment";
var id = "unBCxSc8hR41dg6s6N3d17uccj8jnK5Xsn68C58Y76r9";
var url = "https://docs.google.com/spreadsheets/d/"+id+"/gviz/tq?tq=SELECT%20*%20where%20A%20contains%20%22"+word+"%22";
Please tell me what changes should I make to the URL in order to use regex.
For example, I need to find all rows with pattern co..en.* or .*ment in them.
~·~·~·~·~·~·~·~·~·~·~·~·~·~·~·~·~·~·~·~
EDIT
After Tanaike's help:
var word = "f.*t";
var columna = "A";
var id = "4tz810VLT4qv7Q9t94p24tz810VLT4qv7Q9t94p24tz8";
var url = "https://docs.google.com/spreadsheets/d/"+id+"/gviz/tq?tq=select%20"+columna+"%20where%20A%20matches%20%27"+word+"%27";
This will match fit, faint, font, fruit, feet, fat, etc.
~ finis ~
You want to retrieve the values using the regex of co..en.* or .*ment.
You want to achieve this using the query language of Google Visualization API.
If my understanding is correct, how about this answer? Please think of this as just on of several possible answers.
In this case, how about using matches? The modified query is as follows.
Modified query:
select * where A matches 'co..en.*|.*ment'
In this case, in order to use this query, please encode it with the URL encode. So when your endpoint is modified, please modify as follows.
From:
tq=SELECT%20*%20where%20A%20contains%20%22"+word+"%22"
To:
tq=select%20%2A%20where%20A%20matches%20%27co..en.%2A%7C.%2Ament%27
Reference:
Query Language Reference
If I misunderstood your question and this was not the result you want, I apologize.

How to get the number of rows from a sql query in GAS?

My script allows a user query from a sheet a SQL database. Before letting the script write in the sheet the select's result, how can I know the number of rows I am getting from that query?
Multiplying the count of rows by the count of columns I want to warn the user if the query result size is over 10.000 cells so he is aware of crashing the sheet.
The method getColumnCount() works perfect but not for rows. I tried some other ways but nothing worked... any advice?
var columnsNumber = resultmetadata.getColumnCount();
ui.alert(columnsNumber); //this works
var rowsNumber = resultmetadata.getRowCount();
ui.alert(rowsNumber); //this doesn't work
Thanks,
Gerónimo
Through this post How do I get the size of a java.sql.ResultSet? thanks to Martin Hawksey, I made this and worked perfect:
if (result.last()) { //query result
var rowsNumber = result.getRow();
result.beforeFirst(); // not rs.first() because the rs.next() below will move on, missing the first element
}

oxid import old data SQL

I have an old oxid-version. I exported my old seo-data from the table "oxseo" to get the keywords and description for each article. Now i want to import these fields in my new version of the shop. My articles are already there, but not the seodata.
My first idea was to collect all the data i need from a csv-export of my old data.
For example, my output array could look something like:
$article = array();
$keywords = array();
$desc = array();
foreach($line as $l) {
$keywords[$i] = current_keyword
$desc[$i] = current_description
$oxid[$i] = current_oxid
}
So lets just assume I already have my filled array.
If i check the oxid's, they are still the same. So, from my exported CSV, picking a random OXID, looking for it in my new DB shows me the correct article.
Now my first thought was, to look in oxobject2seodata. I know that the data for the articles are stored in there, but i can't find a way to connect those, since the "oxid" from the old version is not the same as the objectId in the new version. In oxarticles, however, there is no "objectId".
Thank you in advance for any hints and tips
The field OXID in oxarticles table should match the field OXOBJECTID in oxobject2seodata table.
SELECT oa.OXID, o2s.* from oxobject2seodata o2s, oxarticles oa WHERE o2s.OXOBJECTID = oa.OXID AND oa.OXID = '[OXID-of-article]';
-- or
SELECT o2s.* from oxobject2seodata o2s WHERE o2s.OXOBJECTID = '[OXID-of-article]';

google adwords script - find accounts with label

We have many accounts we manage and the ones we are interested have the label 'Active Clients'. This label is shown on the first page of the MCC ('My Client Center') in the table. It is the second column of the table that shows 'Labels'.
Here's the basic script:
function main() {
var accountSelector = MccApp.accounts()
.withCondition("Labels = 'Active Clients'");
var accountIterator = accountSelector.get();
// Iterate through the list of accounts
while (accountIterator.hasNext()) {
var account = accountIterator.next();
// Select the client account.
MccApp.select(account);
// Operate on client account
Logger.log('Customer ID: ' + account.getCustomerId() );
}
}
This is how I'm trying to iterate through all clients that have the label 'Active Clients'. It doesn't work and I'm wondering if it can be made to work or I must specify the id's of all clients.
The column name is LabelNames instead of Labels. Also its type is StringSet, not String, so you should use .withCondition("LabelNames CONTAINS_ANY ['Active Clients']"). However, "LabelNames" is not an available column for AccountSelector conditions yet (https://developers.google.com/adwords/scripts/docs/reference/mccapp/mccapp_accountselector#withCondition_1), hope this will change by time.
Anyway, I recommend to create a new MCC, where you keep only active clients, so you can place the script there without using withCondition.

Return newly inserted row without having to Submit to database

I need to find a way to get the newly insert row, without previously having to save to the database.
Is there a way? Or I need to keep the whole collection of row in a separated array?
Is this example I adding a row to the table tblConfig, but when I look back in the table the new row is not there.
tblConfig Config = new tblConfig { ID = Guid.NewGuid(), Code ="new config code" };
CTX.tblConfig.InsertOnSubmit(Config);
var Data = from dd in CTX.tblConfig select dd;
this.dataGridView1.DataSource = Data;
After some research , I'll do the work by attaching my LINQ query to a BindingSource object, witch will help me handle with insertion update and so.
Thanks for everyone, for your help :)
Hugo
Could you add that manually like this?
this.dataGridView1.DataSource = CTX.tblConfig.Execute(MergeOption.AppendOnly);