wann Import Contacts from a CSV File with Google Apps Script
the CSV File is stored on Google Drive.
I found this example:
But did not work for me:
function importCSVFromGoogleDrive() {
var file = DriveApp.getFilesByName('file.csv').next();
Logger.log(file.getSize());
Logger.log(file.getName());
Logger.log(file.getDateCreated());
var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
got this error:
14:45:50
Hinweis
Ausführung begonnen
14:45:51
Info
1624.0
14:45:51
Info
file.csv
14:45:51
Info
Fri Sep 17 11:51:35 GMT+02:00 2021
14:45:51
Fehler
TypeError: Cannot read property 'getRange' of null
importCSVFromGoogleDrive
# import.gs:9
How to fix this ?
If i set this line:
Logger.log(Utilities.parseCsv(file.getBlob().getDataAsString()));
I can see the content of the CSV file.
15:27:26
Hinweis
Ausführung begonnen
15:27:26
Info
1624.0
15:27:26
Info
file.csv
15:27:26
Info
Fri Sep 17 11:51:35 GMT+02:00 2021
15:27:27
Info
[[Name, Given Name, Additional Name, Family Name, Yomi Name, Given Name Yomi, Additional Name Yomi, Family Name Yomi, Name Prefix, Name Suffix, Initials, Nickname, Short Name, Maiden Name, Birthday, Gender, Location, Billing Information, Directory Server, Mileage, Occupation, Hobby, Sensitivity, Priority, Subject, Notes, Language, Photo, Group Membership, E-mail 1 - Type, E-mail 1 - Value, Phone 1 - Type, Phone 1 - Value, Address 1 - Type, Address 1 - Formatted, Address 1 - Street, Address 1 - City, Address 1 - PO Box, Address 1 - Region, Address 1 - Postal Code, Address 1 - Country, Address 1 - Extended Address, Organization 1 - Type, Organization 1 - Name, Organization 1 - Yomi Name, Organization 1 - Title, Organization 1 - Department, Organization 1 - Symbol, Organization 1 - Location, Organization 1 - Job Description, Website 1 - Type, Website 1 - Value], [Anna Musterfrau, Anna, , Musterfrau, , , , , , , , , , , , , , , , , , , , , , , , , * myContacts, * Home, anna#musterfrau.de, Mobile, +491234567890, , , , , , , , , , , , , , , , , , , ], [Hans Meise, Hans, , Meise, , , , , , , , , , , , , , , , , , , , , , , , , * myContacts, * Home, meinse#hans.de, , , , , , , , , , , , , , , , , , , , , ], [Hans Mustermann, Hans, , Mustermann, , , , , , , , , , , , , , , , , , , , , , , , , * myContacts, * , hans#mustermann.de, Main, +4912345 98765, , Nebenstrasse 122, , , , , , , , , , , , , , , , , ], [Musterhausen 12345, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ], [DE", Nebenstrasse 122, Musterhausen, , , 12345, DE, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ]
How do you run this script?
on https://script.google.com/home with "execude"
the script should run automated with a trigger
so that the contacts will add every day from the CSV file
a script to delete all conacts I found an this work.
But need to add the contacts from CSV file...
Contacts-Source is CSV File, automated import with apps script
every day by using a trigger
Plan is: CSV --> direct Google Contacts Import with Apps Script by using a trigger
I have created sample database
create table items (Order_date date, frame char(2) , group_name varchar(50) , order_qty int(4) , receive_qty int(4));
insert into items(Order_date , frame , group_name , order_qty ,receive_qty) values
("2021-3-10" , "am" , "books" , 4280 , 4000),
("2021-3-12" , "pm" , "notebooks" , 3259 , 3100),
("2021-3-14" , "pm" , "erasers" , 2828 , 2500),
("2021-3-15" , "am" , "books" , 3147 , 3088),
("2021-3-16" , "pm" , "notebooks" , 2897 , 2700),
("2021-3-19" , "am" , "notebooks" , 4793 , 3030),
("2021-3-21" , "pm" , "erasers" , 3317 , 3100);
#Query to get Fill Rate
select group_name , (sum(receive_qty)/sum(order_qty)*100) as Fill_Rate
from items
group by group_name;
#Query to get Fill Rate when frame is AM
select group_name , (sum(receive_qty)/sum(order_qty)*100) as AM_Fill_Rate
from items
where frame = "am"
group by group_name;
#Query to get Fill Rate post 16 mar 2021
select group_name , (sum(receive_qty)/sum(order_qty)*100) as Fill_Rate_post_16Mar
from items
where Order_date >= "2021-03-16"
group by group_name;
How can I get Fill_Rate , AM_Fill_Rate and Fill_Rate_post_16Mar in single table in single run??
since i can get all 3 by individually running above 3 queries , 3 times
Please suggest !
select group_name ,
(sum(receive_qty)/sum(order_qty)*100) as Fill_Rate ,
(sum(case when frame = "am" then receive_qty else 0 end)/sum(case when frame = "am" then order_qty else 0 end)*100) as AM_Fill_Rate
(sum(case when Order_date >= "2021-03-16" then receive_qty else 0 end)/sum(case when Order_date >= "2021-03-16" then order_qty else 0 end)*100) as Fill_Rate_post_16Mar
from items
group by group_name;
We need result from Table based on datetime filter. But it working slow in mysql. I can't implement indexing on date & timestamp columns due to it will slow our insertion/updation. So can you please suggest any alternate for select the data quickly based on date and datetime filter with better performance.
SQL Query :
SELECT *
FROM (
SELECT id
, title
, language
, lang_code
, financial
, fname
, lname
, mname
, mname_br
, suffix
, CASE WHEN DOB='0000-00-00' THEN NULL ELSE DOB END AS DOB
, street
, street2
, postal_code
, zip_ext
, city
, state
, country_code
, phone_home
, phone_biz
, phone_biz_ext
, phone_contact
, phone_cell
, status
, CASE WHEN date='0000-00-00 00:00:00' THEN NULL ELSE
CAST(date as datetime) END AS date
, sex
, referrer
, referrerID
, providerID
, ethnoracial
, pid
, temp_key
, primary_care
, default_facility
, created_by
, patientStatus
, primary_care_id
, Sec_HCFA
, noBalanceBill
, erx_entry
, erx_patient_id
, athenaID
, CASE WHEN licenseDate='0000-00-00 00:00:00' THEN NULL ELSE licenseDate end as licenseDate
, race
, otherRace
, ethnicity
, otherEthnicity
, primary_care_phy_name
, primary_care_phy_id
, CASE WHEN dod_patient='0000-00-00' THEN NULL ELSE dod_patient END AS dod_patient--
, locked--
, co_man_phy--
, co_man_phy_id--
, vip--
, External_MRN_1--
, External_MRN_2--
, External_MRN_3--
, External_MRN_4
, as_id
, CASE WHEN acc_statement_date='0000-00-00' THEN acc_statement_date END AS acc_statement_date
, CASE WHEN timestamp='0000-00-00 00:00:00' THEN NULL ELSE timestamp END AS timestamp
, api_id
, fmh_pt_status
, race_code
, ethnicity_code
, patient_payer
, CASE WHEN date='0000-00-00 00:00:00' THEN NULL ELSE date END AS transfer_created
,CASE WHEN timestamp='0000-00-00 00:00:00' THEN NULL ELSE timestamp END AS transfer_updated
,CASE WHEN date > '2020-11-10 00:00:00' THEN 'new' ELSE 'changed' END AS flagfield
,CASE WHEN date='0000-00-00 00:00:00' THEN NULL ELSE date END AS sortdate
FROM patient_data
WHERE (date > '2020-11-10 00:00:00' or timestamp > '2019-04-01 19:53:57-04')
AND month(date) > 0)t
ORDER BY flagfield desc,
sortdate;
)
id Column has indexing in the table
Get rid of
SELECT *
FROM (
)t
it adds nothing, except to slow things down.
Let's focus on
SELECT id
FROM patient_data
WHERE (date > '2020-11-10 00:00:00'
or timestamp > '2019-04-01 19:53:57-04')
AND month(date) > 0
Does that shortened query run "too slow"? If not, then we can use that as a derived table to see if that will speed it up. If so, then we will need to get into UNION and indexing.
in MySQL query if I pass:
case guides.Gud_Publish_Date
when null then "Unscheduled"
else "Forth Coming Titles"
end
then it is considering all is null even the Gud_Publish_Date has value also. the full SQL statement is
SELECT guides.Gud_Id
, guides.Gud_Image
, guides.Gud_SubEditor
, guides.Gud_Reprint_Status
, guides.Gud_Publish_Date
, guides.Gud_Img_Chk
, guides.Gud_Published
, guides.Gud_View
, (
CASE guides.Gud_Publish_Date
WHEN NULL
THEN "Unscheduled"
ELSE "Forth Coming Titles"
END
) AS Schedules
FROM guides
try using IF
SELECT guides.Gud_Id
, guides.Gud_Image
, guides.Gud_SubEditor
, guides.Gud_Reprint_Status
, guides.Gud_Publish_Date
, guides.Gud_Img_Chk
, guides.Gud_Published
, guides.Gud_View
, IF(guides.Gud_Publish_Date IS NULL,'Unscheduled','Forth Coming Titles')
AS Schedules
FROM guides
or if you really want CASE
SELECT guides.Gud_Id
, guides.Gud_Image
, guides.Gud_SubEditor
, guides.Gud_Reprint_Status
, guides.Gud_Publish_Date
, guides.Gud_Img_Chk
, guides.Gud_Published
, guides.Gud_View
, (
CASE
WHEN guides.Gud_Publish_Date IS NULL
THEN 'Unscheduled'
ELSE 'Forth Coming Titles'
END
) AS Schedules
FROM guides
I found this -a couple of months- old post. Using COALESCE option as intended by Rajan, you can do,
SELECT guides.Gud_Id
, guides.Gud_Image
, guides.Gud_SubEditor
, guides.Gud_Reprint_Status
, guides.Gud_Publish_Date
, guides.Gud_Img_Chk
, guides.Gud_Published
, guides.Gud_View
, CASE COALESCE(guides.Gud_Publish_Date, 0)
WHEN 0 THEN 'Unscheduled'
ELSE 'Forth Coming Titles'
END AS Schedules
FROM guides
The code above assumes that guides.Gud_Publish_Date cannot take the value 0, which I can do because it is a date. If that weren't the case you can change 0 for another value that it cannot take; maybe your favorite float like 3.1415 or a null identifier 'null'.
SELECT guides.Gud_Id
, guides.Gud_Image
, guides.Gud_SubEditor
, guides.Gud_Reprint_Status
, guides.Gud_Publish_Date
, guides.Gud_Img_Chk
, guides.Gud_Published
, guides.Gud_View
, (
CASE WHEN guides.Gud_Publish_Date IS NULL
THEN "Unscheduled"
ELSE "Forth Coming Titles"
END
) AS Schedules
FROM guides
Try this
SELECT guides.Gud_Id
, guides.Gud_Image
, guides.Gud_SubEditor
, guides.Gud_Reprint_Status
, guides.Gud_Publish_Date
, guides.Gud_Img_Chk
, guides.Gud_Published
, guides.Gud_View
, coalesce((
CASE guides.Gud_Publish_Date
WHEN NULL
THEN 'Unscheduled'
ELSE 'Forth Coming Titles'
END
), 'Unscheduled') AS Schedules
FROM guides