Advertisement
anonydee

Untitled

Jun 16th, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | Source Code | 0 0
  1. function createContactsFromEmptyRows() {
  2. var ss = SpreadsheetApp.getActiveSpreadsheet()
  3. var sheetName = "Free Trial Class (Contacts)";
  4. var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  5. var dataRange = sheet.getDataRange();
  6. var dataValues = dataRange.getValues();
  7. var lastRow = dataRange.getLastRow();
  8. var statusColumn = 14; // Column N
  9. var trialTypeColumn = 8; // Column H
  10.  
  11. var contactsCreatedCount = 0;
  12.  
  13. for (var row = 1; row < lastRow; row++) {
  14. var rowData = dataValues[row];
  15.  
  16. // Check if the N column is empty
  17. if (!rowData[statusColumn - 1]) {
  18. // Retrieve the necessary data for creating the contact
  19. var emailAddress = rowData[0];
  20. var studentName = rowData[1];
  21. var parentName = rowData[3];
  22. var phoneNumber = rowData[4];
  23. var studentAge = rowData[2];
  24. var trialDate = rowData[5];
  25. var parentAttendance = rowData[6];
  26. var trialType = rowData[trialTypeColumn - 1];
  27.  
  28. var resourceName;
  29.  
  30. try {
  31. var contact = People.People.createContact({
  32. "names": [
  33. {
  34. "givenName": `${trialType == "SSC"?"[SSC]":""} ${parentName} - ${studentName} (${trialDate.split(" ")[0]})`,
  35. }
  36. ],
  37. "phoneNumbers": [
  38. {
  39. "value": phoneNumber,
  40. "type": "home"
  41. }
  42. ],
  43. "emailAddresses": [
  44. {
  45. "value": emailAddress,
  46. "type": "home"
  47. }
  48. ],
  49. "userDefined": [
  50. {
  51. "key": "Student's Age",
  52. "value": studentAge
  53. },
  54. {
  55. "key": "请选择 魔术方块体验课 Free Trial 的日期",
  56. "value": trialDate
  57. },
  58. {
  59. "key": "家长是否会和孩子一起参加 体验课 Free Trial",
  60. "value": parentAttendance
  61. },
  62. {
  63. "key": "Free Trial",
  64. "value": trialType
  65. }
  66. ]
  67. });
  68.  
  69. if (contact.resourceName) {
  70. resourceName = contact.resourceName;
  71. }
  72. } catch (error) {
  73. console.error(error);
  74. }
  75.  
  76. if (resourceName) {
  77. // Contact creation successful
  78.  
  79. // Format the contact name
  80. var formattedContactName = `Ok Contacts`;
  81.  
  82. // Generate the hyperlink using the returned resource
  83. var hyperlink = `https://contacts.google.com/person/${resourceName.replace("people/", "")}`;
  84.  
  85. // Set the rich text value for the status column
  86. var richTextValue = SpreadsheetApp.newRichTextValue()
  87. .setText(formattedContactName)
  88. .setLinkUrl(hyperlink)
  89. .build();
  90. sheet.getRange(row + 1, statusColumn).setRichTextValue(richTextValue);
  91.  
  92. contactsCreatedCount++;
  93. } else {
  94. // Contact creation failed
  95. var failureReason = "Contact creation failed";
  96. var failedStatus = `FAILED: ${failureReason}`;
  97.  
  98. // Set the failed status without modifying the existing hyperlink
  99. var existingRichTextValue = sheet.getRange(row + 1, statusColumn).getRichTextValue();
  100. var modifiedRichTextValue = SpreadsheetApp.newRichTextValue()
  101. .setText(failedStatus)
  102. .setLinkUrl(existingRichTextValue.getLinkUrl())
  103. .build();
  104. sheet.getRange(row + 1, statusColumn).setRichTextValue(modifiedRichTextValue);
  105. }
  106. }
  107. if(row%5 == 0) {
  108. Utilities.sleep(4000);
  109. ss.toast("Please wait", "Processing " + row + " row")
  110. }
  111. }
  112.  
  113. Logger.log(`Contacts created: ${contactsCreatedCount}`);
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement