EditGoogleSheets.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <template>
  2. <div class="mb-10">
  3. <ui-textarea
  4. :model-value="data.description"
  5. class="mb-2 w-full"
  6. :placeholder="t('common.description')"
  7. @change="updateData({ description: $event })"
  8. />
  9. <ui-select
  10. :model-value="data.type"
  11. class="mb-2 w-full"
  12. @change="onActionChange"
  13. >
  14. <option v-for="action in actions" :key="action" :value="action">
  15. {{ t(`workflow.blocks.google-sheets.select.${action}`) }}
  16. </option>
  17. </ui-select>
  18. <slot />
  19. <edit-autocomplete
  20. v-if="!googleDrive || data.inputSpreadsheetId === 'manually'"
  21. >
  22. <ui-input
  23. :model-value="data.spreadsheetId"
  24. class="w-full"
  25. placeholder="abcd123"
  26. @change="updateData({ spreadsheetId: $event }), checkPermission($event)"
  27. >
  28. <template #label>
  29. {{ t('workflow.blocks.google-sheets.spreadsheetId.label') }}*
  30. <a
  31. href="https://docs.automa.site/blocks/google-sheets.html#spreadsheet-id"
  32. target="_blank"
  33. rel="noopener"
  34. :title="t('workflow.blocks.google-sheets.spreadsheetId.link')"
  35. >
  36. <v-remixicon name="riInformationLine" size="18" class="inline" />
  37. </a>
  38. </template>
  39. </ui-input>
  40. </edit-autocomplete>
  41. <a
  42. v-if="!state.havePermission"
  43. href="https://docs.automa.site/blocks/google-sheets.html#access-to-spreadsheet"
  44. target="_blank"
  45. rel="noopener"
  46. class="ml-1 inline-block text-sm leading-tight"
  47. >
  48. Automa doesn't have access to the spreadsheet
  49. <v-remixicon name="riInformationLine" size="18" class="inline" />
  50. </a>
  51. <edit-autocomplete v-if="data.type !== 'create'">
  52. <ui-input
  53. :model-value="data.range"
  54. class="mt-1 w-full"
  55. placeholder="Sheet1!A1:B2"
  56. @change="updateData({ range: $event })"
  57. >
  58. <template #label>
  59. {{ t('workflow.blocks.google-sheets.range.label') }}*
  60. <a
  61. href="https://docs.automa.site/blocks/google-sheets.html#range"
  62. target="_blank"
  63. rel="noopener"
  64. :title="t('workflow.blocks.google-sheets.range.link')"
  65. >
  66. <v-remixicon name="riInformationLine" size="18" class="inline" />
  67. </a>
  68. </template>
  69. </ui-input>
  70. </edit-autocomplete>
  71. <template v-if="data.type === 'get'">
  72. <ui-input
  73. :model-value="data.refKey"
  74. :label="t('workflow.blocks.google-sheets.refKey.label')"
  75. :placeholder="t('workflow.blocks.google-sheets.refKey.placeholder')"
  76. class="mt-1 w-full"
  77. @change="updateData({ refKey: $event })"
  78. />
  79. <ui-checkbox
  80. :model-value="data.firstRowAsKey"
  81. class="mt-3"
  82. @change="updateData({ firstRowAsKey: $event })"
  83. >
  84. {{ t('workflow.blocks.google-sheets.firstRow') }}
  85. </ui-checkbox>
  86. <ui-button
  87. :loading="previewDataState.status === 'loading'"
  88. variant="accent"
  89. class="mt-3"
  90. @click="previewData"
  91. >
  92. {{ t('workflow.blocks.google-sheets.previewData') }}
  93. </ui-button>
  94. <p v-if="previewDataState.status === 'error'" class="text-red-500">
  95. {{ previewDataState.errorMessage }}
  96. </p>
  97. </template>
  98. <template v-else-if="['getRange', 'create'].includes(data.type)">
  99. <p class="mt-4">
  100. {{ t('workflow.blocks.google-sheets.spreadsheetId.label') }}
  101. </p>
  102. <insert-workflow-data :data="data" variables @update="updateData" />
  103. <ui-button
  104. v-if="data.type === 'getRange'"
  105. :loading="previewDataState.status === 'loading'"
  106. variant="accent"
  107. class="mt-4"
  108. @click="previewData"
  109. >
  110. {{ t('workflow.blocks.google-sheets.previewData') }}
  111. </ui-button>
  112. </template>
  113. <template v-else-if="['update', 'append'].includes(data.type)">
  114. <ui-select
  115. :model-value="data.valueInputOption"
  116. class="mt-2 w-full"
  117. @change="updateData({ valueInputOption: $event })"
  118. >
  119. <template #label>
  120. {{ t('workflow.blocks.google-sheets.valueInputOption') }}
  121. <a
  122. href="https://developers.google.com/sheets/api/reference/rest/v4/ValueInputOption"
  123. target="_blank"
  124. rel="noopener"
  125. >
  126. <v-remixicon name="riInformationLine" size="18" class="inline" />
  127. </a>
  128. </template>
  129. <option
  130. v-for="option in valueInputOptions"
  131. :key="option"
  132. :value="option"
  133. >
  134. {{ option }}
  135. </option>
  136. </ui-select>
  137. <ui-select
  138. v-if="data.type === 'append'"
  139. :model-value="data.insertDataOption || 'INSERT_ROWS'"
  140. class="mt-2 w-full"
  141. @change="updateData({ insertDataOption: $event })"
  142. >
  143. <template #label>
  144. {{ t('workflow.blocks.google-sheets.insertDataOption') }}
  145. <a
  146. href="https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append#InsertDataOption"
  147. target="_blank"
  148. rel="noopener"
  149. >
  150. <v-remixicon name="riInformationLine" size="18" class="inline" />
  151. </a>
  152. </template>
  153. <option
  154. v-for="option in insertDataOptions"
  155. :key="option"
  156. :value="option"
  157. >
  158. {{ option }}
  159. </option>
  160. </ui-select>
  161. <ui-select
  162. :model-value="data.dataFrom"
  163. :label="t('workflow.blocks.google-sheets.dataFrom.label')"
  164. class="mt-2 w-full"
  165. @change="updateData({ dataFrom: $event })"
  166. >
  167. <option v-for="item in dataFrom" :key="item" :value="item">
  168. {{ t(`workflow.blocks.google-sheets.dataFrom.options.${item}`) }}
  169. </option>
  170. </ui-select>
  171. <ui-checkbox
  172. v-if="data.dataFrom === 'data-columns'"
  173. :model-value="data.keysAsFirstRow"
  174. class="mt-2"
  175. @change="updateData({ keysAsFirstRow: $event })"
  176. >
  177. {{ t('workflow.blocks.google-sheets.keysAsFirstRow') }}
  178. </ui-checkbox>
  179. <ui-button
  180. v-else
  181. class="mt-2 w-full"
  182. variant="accent"
  183. @click="customDataState.showModal = true"
  184. >
  185. {{ t('workflow.blocks.google-sheets.insertData') }}
  186. </ui-button>
  187. </template>
  188. <shared-codemirror
  189. v-if="
  190. previewDataState.data &&
  191. previewDataState.status !== 'error' &&
  192. data.type !== 'update'
  193. "
  194. :model-value="previewDataState.data"
  195. :line-numbers="false"
  196. readonly
  197. hide-lang
  198. class="scroll mt-4 max-h-96"
  199. />
  200. <ui-modal
  201. v-model="customDataState.showModal"
  202. title="Custom data"
  203. content-class="max-w-xl"
  204. >
  205. <shared-codemirror
  206. v-model="customDataState.data"
  207. style="height: calc(100vh - 10rem)"
  208. lang="json"
  209. @change="updateData({ customData: $event })"
  210. />
  211. </ui-modal>
  212. </div>
  213. </template>
  214. <script setup>
  215. import { shallowReactive, defineAsyncComponent } from 'vue';
  216. import { useI18n } from 'vue-i18n';
  217. import { useToast } from 'vue-toastification';
  218. import { fetchApi } from '@/utils/api';
  219. import { convert2DArrayToArrayObj, debounce } from '@/utils/helper';
  220. import googleSheetsApi from '@/utils/googleSheetsApi';
  221. import EditAutocomplete from './EditAutocomplete.vue';
  222. import InsertWorkflowData from './InsertWorkflowData.vue';
  223. const SharedCodemirror = defineAsyncComponent(() =>
  224. import('@/components/newtab/shared/SharedCodemirror.vue')
  225. );
  226. const props = defineProps({
  227. data: {
  228. type: Object,
  229. default: () => ({}),
  230. },
  231. googleDrive: Boolean,
  232. additionalActions: {
  233. type: Array,
  234. default: () => [],
  235. },
  236. });
  237. const emit = defineEmits(['update:data']);
  238. const { t } = useI18n();
  239. const toast = useToast();
  240. const actions = [
  241. 'get',
  242. 'getRange',
  243. 'update',
  244. 'append',
  245. 'clear',
  246. ...props.additionalActions,
  247. ];
  248. const dataFrom = ['data-columns', 'custom'];
  249. const valueInputOptions = ['RAW', 'USER_ENTERED'];
  250. const insertDataOptions = ['OVERWRITE', 'INSERT_ROWS'];
  251. const previewDataState = shallowReactive({
  252. data: '',
  253. status: 'idle',
  254. errorMessage: '',
  255. });
  256. const customDataState = shallowReactive({
  257. showModal: false,
  258. data: props.data.customData,
  259. });
  260. const state = shallowReactive({
  261. lastSheetId: null,
  262. havePermission: true,
  263. });
  264. const checkPermission = debounce(async (value) => {
  265. try {
  266. if (state.lastSheetId === value) return;
  267. const response = await fetchApi(
  268. `/services/google-sheets/meta?spreadsheetId=${value}`
  269. );
  270. state.havePermission = response.status !== 403;
  271. state.lastSheetId = value;
  272. } catch (error) {
  273. console.error(error);
  274. }
  275. }, 1000);
  276. function updateData(value) {
  277. emit('update:data', { ...props.data, ...value });
  278. }
  279. async function previewData() {
  280. try {
  281. previewDataState.status = 'loading';
  282. const isGetValues = props.data.type === 'get';
  283. const params = {
  284. range: props.data.range,
  285. spreadsheetId: props.data.spreadsheetId,
  286. };
  287. if (!props.data.spreadsheetId) {
  288. toast.error(
  289. props.googleDrive
  290. ? 'No spreadsheet is selected'
  291. : 'Spreadsheet id is empty'
  292. );
  293. previewDataState.status = 'idle';
  294. return;
  295. }
  296. if (!props.data.range) {
  297. toast.error('Spreadsheet range is empty');
  298. previewDataState.status = 'idle';
  299. return;
  300. }
  301. const response = await (isGetValues
  302. ? googleSheetsApi(props.googleDrive).getValues(params)
  303. : googleSheetsApi(props.googleDrive).getRange(params));
  304. let result = props.googleDrive ? response : await response.json();
  305. if (!response.ok && !props.googleDrive) {
  306. throw new Error(result.message || response.statusText);
  307. }
  308. if (isGetValues) {
  309. const values = result?.values ?? [];
  310. result = props.data.firstRowAsKey
  311. ? convert2DArrayToArrayObj(values)
  312. : values;
  313. } else {
  314. result = {
  315. tableRange: result.tableRange || null,
  316. lastRange: result.updates.updatedRange,
  317. };
  318. }
  319. previewDataState.data = JSON.stringify(result, null, 2);
  320. previewDataState.status = 'idle';
  321. } catch (error) {
  322. console.error(error);
  323. previewDataState.data = '';
  324. previewDataState.status = 'error';
  325. previewDataState.errorMessage = error.message;
  326. }
  327. }
  328. function onActionChange(value) {
  329. updateData({ type: value });
  330. previewDataState.data = '';
  331. previewDataState.status = '';
  332. previewDataState.errorMessage = '';
  333. }
  334. </script>