[Bulk operations]. 3 Level of working with multiple files and entries

Level 3 - Google Sheet for bulk run your Flow

We will be running your flow for each row in A column containing the data (url).

  • There will be some code, but you can copy-paste-it
  • It is highly required to understand what is API request and why do we need it

Ok, so let’s dig into Google Sheets x Scade option.
Attention, so far there is no native integration and a pretty haptic API requests management.

Step 1 - Create your server API key

Go to API keys page and create new Server Key

Step 2 - Copy your Flow HTTP request

Open your flow, click publish

Click on API in Extended settings sidebar

Step 4 - Create your Google Sheet

Let’s create a sample sheet and fill it with our URls

Step 5 - Open App Script

It lives in Extensions → App Script

Step 6 - Rename your script and paste this code

The Code you need to copy
function runAPIsForEachCell() {
  const token = 'XXXXXXXXXXXXX'; // Your actual token
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const dataRange = sheet.getRange('A2:A'); // Assuming you want to start from row 2.
  const data = dataRange.getValues();

  data.forEach((row, index) => {
    const videoUrl = row[0]; // The value from column A (video_url).

    if (videoUrl) {
      // Run the POST request
      const postResponse = postScadeFlow(videoUrl, token);
      const taskId = postResponse.id; // Extract task_id from POST response
      
      // Check the status of the task periodically
      const getResponse = waitForTaskCompletion(taskId, token);
      
      // Extract the text_review from the response
      const textReview = extractTextReview(getResponse);
      
      // Store the text_review in column B
      sheet.getRange(index + 2, 2).setValue(textReview);
    }
  });
}

function postScadeFlow(videoUrl, token) {
  const url = 'https://app.scade.pro/api/v1/scade/flow/40199/execute';
  const payload = {
    "start_node_id": "axi1-start",
    "end_node_id": "AQ6K-end",
    "result_node_id": "AQ6K-end",
    "node_settings": {
      "axi1-start": {
        "data": {
          "video_url": videoUrl
        }
      }
    }
  };

  const options = {
    method: 'post',
    contentType: 'application/json',
    headers: {
      'Authorization': `Basic ${token}`,
      'Content-Type': 'application/json'
    },
    payload: JSON.stringify(payload)
  };

  const response = UrlFetchApp.fetch(url, options);
  return JSON.parse(response.getContentText()); // Returning the POST response
}

function waitForTaskCompletion(taskId, token) {
  const url = `https://api.scade.pro/api/v1/task/${taskId}`;
  const options = {
    method: 'get',
    headers: {
      'Authorization': `Basic ${token}`,
      'Content-Type': 'application/json'
    }
  };
  
  let isCompleted = false;
  let response;
  
  // Check task status every 5 seconds for up to 2 minutes (adjust as necessary)
  for (let i = 0; i < 100; i++) {  // 24 attempts, 5 seconds each = 2 minutes max
    Utilities.sleep(5000); // Wait for 5 seconds before checking the status again
    response = UrlFetchApp.fetch(url, options);
    const taskStatus = JSON.parse(response.getContentText());
    
    // Check if the task has been completed
    if (taskStatus.status === 'completed' || taskStatus.state === 'done' || taskStatus.result) {
      isCompleted = true;
      break;
    }
  }
  
  return JSON.parse(response.getContentText()); // Return the final GET response when completed
}

function extractTextReview(response) {
  // Navigate the response to find the text_review
  try {
    const textReview = response.result.success.text_review;
    // Clean up any unwanted characters if needed
    return textReview.replace(/\\n/g, ' ').trim();
  } catch (e) {
    return 'No text_review found';
  }
}

Step 7 - Paste your Server Token

Step 8 - Save and run!


image

Step 9 - Allow Script to interact with your Spreadsheet

Google will ask you if you really sure you wanna trust this dev (Yourself, lol) and give an access to your files. Now worries, it happens inside your account and inside your sheet.

Also try to analyze this code with GPT and ask if it’s safe to run it in Google App Script.

Step 10 - Waiting

When you try to bulk run heavy flows - keep calm and wait. Each run took around 300s, so you’d need to wait a bit to finish your transcribation.

If you are not sure that API request has been successfully sent to Scade - open Run History
(this icon)

4 Likes