Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function createContactsFromEmptyRows() {
- var ss = SpreadsheetApp.getActiveSpreadsheet()
- var sheetName = "Free Trial Class (Contacts)";
- var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
- var dataRange = sheet.getDataRange();
- var dataValues = dataRange.getValues();
- var lastRow = dataRange.getLastRow();
- var statusColumn = 14; // Column N
- var trialTypeColumn = 8; // Column H
- var contactsCreatedCount = 0;
- for (var row = 1; row < lastRow; row++) {
- var rowData = dataValues[row];
- // Check if the N column is empty
- if (!rowData[statusColumn - 1]) {
- // Retrieve the necessary data for creating the contact
- var emailAddress = rowData[0];
- var studentName = rowData[1];
- var parentName = rowData[3];
- var phoneNumber = rowData[4];
- var studentAge = rowData[2];
- var trialDate = rowData[5];
- var parentAttendance = rowData[6];
- var trialType = rowData[trialTypeColumn - 1];
- var resourceName;
- try {
- var contact = People.People.createContact({
- "names": [
- {
- "givenName": `${trialType == "SSC"?"[SSC]":""} ${parentName} - ${studentName} (${trialDate.split(" ")[0]})`,
- }
- ],
- "phoneNumbers": [
- {
- "value": phoneNumber,
- "type": "home"
- }
- ],
- "emailAddresses": [
- {
- "value": emailAddress,
- "type": "home"
- }
- ],
- "userDefined": [
- {
- "key": "Student's Age",
- "value": studentAge
- },
- {
- "key": "请选择 魔术方块体验课 Free Trial 的日期",
- "value": trialDate
- },
- {
- "key": "家长是否会和孩子一起参加 体验课 Free Trial",
- "value": parentAttendance
- },
- {
- "key": "Free Trial",
- "value": trialType
- }
- ]
- });
- if (contact.resourceName) {
- resourceName = contact.resourceName;
- }
- } catch (error) {
- console.error(error);
- }
- if (resourceName) {
- // Contact creation successful
- // Format the contact name
- var formattedContactName = `Ok Contacts`;
- // Generate the hyperlink using the returned resource
- var hyperlink = `https://contacts.google.com/person/${resourceName.replace("people/", "")}`;
- // Set the rich text value for the status column
- var richTextValue = SpreadsheetApp.newRichTextValue()
- .setText(formattedContactName)
- .setLinkUrl(hyperlink)
- .build();
- sheet.getRange(row + 1, statusColumn).setRichTextValue(richTextValue);
- contactsCreatedCount++;
- } else {
- // Contact creation failed
- var failureReason = "Contact creation failed";
- var failedStatus = `FAILED: ${failureReason}`;
- // Set the failed status without modifying the existing hyperlink
- var existingRichTextValue = sheet.getRange(row + 1, statusColumn).getRichTextValue();
- var modifiedRichTextValue = SpreadsheetApp.newRichTextValue()
- .setText(failedStatus)
- .setLinkUrl(existingRichTextValue.getLinkUrl())
- .build();
- sheet.getRange(row + 1, statusColumn).setRichTextValue(modifiedRichTextValue);
- }
- }
- if(row%5 == 0) {
- Utilities.sleep(4000);
- ss.toast("Please wait", "Processing " + row + " row")
- }
- }
- Logger.log(`Contacts created: ${contactsCreatedCount}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement